German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Set Text of a Label inside a DataList's item template from code behind
Written By Omer Kamal On 06/02/2008

Assigning Text to a Label Control embeded inside an ItemTemplate of a DataList

Views: 1727
Rating: 4.375
Login to Rate
omerkamal
Tagged Under: ADO .NET, Data Presentation Controls, Database

Explanation:

We will be able to access the Row item of a Datalist when the DataList is already bounded to the Database. so we will using ItemDataBound event of the DataList.

our DataList with its DataSource Control look like this: 

DataSourceID="SqlDataSource1" OnItemDataBound="DataList1_ItemDataBound">


AlbumID:



Caption:



IsPublic:



UserName:







ConnectionString="<%$ ConnectionStrings:Personal %>"
SelectCommand="SELECT * FROM [Albums]">

 Now the Code behind for ItemDataBound event is the following:

  protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label exam_weigh = (Label)e.Item.FindControl("Label1");
            exam_weigh.Text = "Test String";
        }
    }
                                                

You noticed that we are accessing two ListItemType (Item and AlternatingItem) types from the current row.

Delicious Digg reddit reddit Technorati
About the Author:

@@ Omer Kamal is a Software Developer at Elanize KG Germany. He MSc. Mathematics from Islamia University Bahawalpur, Pakistan and Certified Developer from National Institute of Electronics Islamabad, Pakistan. He is Founder of FriendsPoint.de and Dotnet-Friends.com. He is currently Involved with Microsoft Office SharePoint 2007, Microsoft Dynamics CRM, BI Portal Solutions (Microsoft Dynamics Customization) and Web Security Solutions.
Check Omer Kamal Profile

Related Useful Links:
Visitors/Readers Comments
(for questions please use The Forum)



Trent
Worked perfect!!!  Thank you for the article it has helped me greatly.

29/07/2008 13:19:43 UTC

Roshan
Thanks alot for ur suggetions.

04/11/2008 05:10:10 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)