I have a datalist in my application whose headertemplate has a lable.Now i need to access the lable from codebehind.How can i do that..
CODE:
Attach OnItemDataBound
event with your datalist like this
<asp:DataList ID="Dlitems" runat="server" RepeatDirection="Horizontal" RepeatColumns="4"
CellPadding="0" CellSpacing="15" OnItemCommand="Dlitems_ItemCommand"
OnItemDataBound="Dlitems_ItemDataBound">
...
And define it like this
protected void Dlitems_ItemDataBound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Label lblCat = (Label)e.Item.FindControl("lblcat");
lblCat.Text = "Changed!";
}
}
The above code is correct however you will need to add post back to the code and define it correctly as the user has not click on any buttons or links so we do not want to display Changed unless the user click on the link or button. The as following does that:
protected void dlData_ItemDataBound(object sender, DataListItemEventArgs e)
{
try
{
if (Page.IsPostBack)
{
if (e.Item.ItemType == ListItemType.Header)
{
Label lblCat = (Label)e.Item.FindControl("lblcat");
lblCat.Text = "Changed!";
}
}
}
catch (Exception ex)
{
throw;
}
}
Happy Programming