Saturday, October 10, 2009

How to access a control in Header and footer in DataList

This is Technique used to access the header and footer controls of Datalist

//in design---------------------

<asp:DataList ID="DataList1"
runat="server"
Width="100%"
OnItemCommand="DataList1_ItemCommand1"
HorizontalAlign="Left">
<HeaderTemplate>
<table class="DatalistHeader">
<tr>
<th align="center" style="width: 10%;">Sr.no</th>
<th align="center" style="width: 30%;">Emp Name</th>
<th align="center" style="width: 20%;">Designation</th>
<th align="center" style="width: 8%;">Department</th>
<th align="center" style="width: 10%;">Post</th>
<th align="center" style="width: 12%;">Salary <br /> In Rs..</th>
<th align="center" style="width: 10%;">
<asp:LinkButton ID="lnkCheck_head" runat="server">Check</asp:LinkButton>
</th>
</tr>
</table>
</HeaderTemplate>
<AlternatingItemStyle BackColor="#F1F8FE"></AlternatingItemStyle>
<ItemTemplate>
....
....
....
....
</ItemTemplate> <FooterTemplate>
<table class="DatalistHeader">
<tr>
<th align="center" style="width: 10%;">Sr.no</th>
<th align="center" style="width: 30%;">Emp Name</th>
<th align="center" style="width: 20%;">Designation</th>
<th align="center" style="width: 8%;">Department</th>
<th align="center" style="width: 10%;">Post</th>
<th align="center" style="width: 12%;">Salary <br /> In Rs..</th>
<th align="center" style="width: 10%;">
<asp:LinkButton ID="lnkCheck_foot" runat="server">Check</asp:LinkButton>
</th>
</tr>
</table>
</FooterTemplate>
</asp:DataList>


//----------------------------------

In Code behind..........................


//this is used to get the contents of HEADER like Compare link button
LinkButton Comp_header = (LinkButton) DataList1.Controls[0].Controls[0].FindControl("lnkCheck_head");

int footerIndex = DataList1.Controls[0].Controls.Count - 1;

//this is used to get the contents of FOOTER like Compare link button

LinkButton Comp_footer = (LinkButton) DataList1.Controls[0].Controls[footerIndex].FindControl("lnkCheck_foot");




if (Comp_header != null)
{
//do something

}

if (Comp_footer != null)
{
//do something
}

No comments:

Post a Comment