In this article I will explain with an example, how to find and access Repeater controls with HeaderTemplate and FooterTemplate in ASP.Net using C# and VB.Net.
HTML Markup
The HTML Markup consists of following controls:
Repeater – For displaying data.
The Repeater consists of following Templates:
HeaderTemplate – The HeaderTemplate consist of a Label control.
ItemTemplate – The ItemTemplate consists of Eval function which is used to bind Country data to columns.
FooterTemplate – The FooterTemplate consist of a Label control.
<asp:Repeater ID="rptProducts" runat="server">
<HeaderTemplate>
<asp:Label ID="lblHeader" runat="server" Font-Bold="true" /><br />
</HeaderTemplate>
<ItemTemplate>
<%#Eval("Name") %> - <%#Eval("Country") %><br />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFooter" runat="server" Font-Bold="true" />
</FooterTemplate>
</asp:Repeater>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Find and access the ASP.Net Repeater Header and Footer Template
Inside the Page_Load event handler, the Repeater is populated with dummy data using DataTable.
Then, the Control and Label class object are created and the HeaderTemplate and FooterTemplate is referenced and Header and Footer are set.
Finally, the message is set in HeaderTemplate and FooterTemplate using Label control.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Binding the Repeater control.
DataTable dt = new DataTable();
Dt.Columns.AddRange(new DataColumn[2] {
new DataColumn("Name"),
new DataColumn("Country") });
dt.Rows.Add("John Hammond", "United States");
dt.Rows.Add("Mudassar Khan", "India");
dt.Rows.Add("Suzanne Mathews", "France");
dt.Rows.Add("Robert Schidner", "Russia");
rptProducts.DataSource = dt;
rptProducts.DataBind();
//Finding the HeaderTemplate and access its controls
Control HeaderTemplate = rptProducts.Controls[0];
Label lblHeader = HeaderTemplate.FindControl("lblHeader") as Label;
lblHeader.Text = "Customers";
//Finding the FooterTemplate and access its controls
Control FooterTemplate = rptProducts.Controls[rptProducts.Controls.Count - 1];
Label lblFooter = FooterTemplate.FindControl("lblFooter") as Label;
lblFooter.Text = "www.aspsnippets.com";
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
'Binding the Repeater control.
Dim dt As DataTable = New DataTable()
Dt.Columns.AddRange(New DataColumn(1) {
New DataColumn("Name"),
New DataColumn("Country")})
dt.Rows.Add("John Hammond", "United States")
dt.Rows.Add("Mudassar Khan", "India")
dt.Rows.Add("Suzanne Mathews", "France")
dt.Rows.Add("Robert Schidner", "Russia")
rptProducts.DataSource = dt
rptProducts.DataBind()
'Finding the HeaderTemplate and access its controls.
Dim HeaderTemplate As Control = rptProducts.Controls(0)
Dim lblHeader As Label = TryCast(HeaderTemplate.FindControl("lblHeader"), Label)
lblHeader.Text = "Customers"
'Finding the FooterTemplate and access its controls
Dim FooterTemplate As Control = rptProducts.Controls(rptProducts.Controls.Count - 1)
Dim lblFooter As Label = TryCast(FooterTemplate.FindControl("lblFooter"), Label)
lblFooter.Text = "www.aspsnippets.com"
End If
End Sub
Screenshot
Demo
Downloads