In this article I will explain with an example how to find the ASP.Net Repeater Header and Footer Template in other words ASP.Net Repeater Header Row and Footer Row. I will also explain how we can find the child controls within the Repeater Header Row and Footer Row.
HTML Markup
Below is my simple ASP.Net Repeater control with HeaderTemplate and FooterTemplate. You will find that I have placed a Label control lblHeader in the HeaderTemplate and lblFooter in the FooterTemplate. Later in the article I’ll show you how can we access these Label controls in our code behind and set their Text properties.
<asp:Repeater ID="rptProducts" runat="server">
<HeaderTemplate>
<asp:Label ID="lblHeader" runat="server" Font-Bold = "true" /><br />
</HeaderTemplate>
<ItemTemplate>
<%#Eval("Name") %> - <%#Eval("Price") %><br />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFooter" runat="server" Font-Bold = "true" />
</FooterTemplate>
</asp:Repeater>
Find and access the ASP.Net Repeater Header and Footer Template
In the below code snippet, I am first binding the ASP.Net Repeater control with dummy DataTable. Once the Repeater is bind with the data I am first referencing the HeaderTemplate of the Repeater, after that I am searching for the Label control lblHeader and set its Text.
Same way I do for the Label control lblFooter in the FooterTemplate of the ASP.Net Repeater control.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Binding the Repeater control.
DataTable Items = new DataTable();
Items.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Price") });
DataTable FilteredItems = Items.Clone();
Items.Rows.Add("Shirt", 200);
Items.Rows.Add("Football", 30);
Items.Rows.Add("Bat", 22.5);
rptProducts.DataSource = Items;
rptProducts.DataBind();
//Finding the HeaderTemplate and access its controls
Control HeaderTemplate = rptProducts.Controls[0].Controls[0];
Label lblHeader = HeaderTemplate.FindControl("lblHeader") as Label;
lblHeader.Text = "Header";
//Finding the FooterTemplate and access its controls
Control FooterTemplate = rptProducts.Controls[rptProducts.Controls.Count - 1].Controls[0];
Label lblFooter = FooterTemplate.FindControl("lblFooter") as Label;
lblFooter.Text = "Footer";
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
'Binding the Repeater control.
Dim Items As New DataTable()
Items.Columns.AddRange(New DataColumn(1) {New DataColumn("Name"), New DataColumn("Price")})
Dim FilteredItems As DataTable = Items.Clone()
Items.Rows.Add("Shirt", 200)
Items.Rows.Add("Football", 30)
Items.Rows.Add("Bat", 22.5)
rptProducts.DataSource = Items
rptProducts.DataBind()
'Finding the HeaderTemplate and access its controls
Dim HeaderTemplate As Control = rptProducts.Controls(0).Controls(0)
Dim lblHeader As Label = TryCast(HeaderTemplate.FindControl("lblHeader"), Label)
lblHeader.Text = "Header"
'Finding the FooterTemplate and access its controls
Dim FooterTemplate As Control = rptProducts.Controls(rptProducts.Controls.Count - 1).Controls(0)
Dim lblFooter As Label = TryCast(FooterTemplate.FindControl("lblFooter"), Label)
lblFooter.Text = "Footer"
End If
End Sub
Downloads