Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to find controls in Header and Footer Template of Repeater control in ASP.NET

Author: Rudy Pinson
Download Source Code :

I will show you in this article that how you can find controls in HeaderTemplate and FooterTemplate of Repeater control in ASP.NET.


I have explained about Repeater control and how we can bind values to repeater control in my previous article on DevASP.NET. Repeater control Template driven control as you can bind repeated items in ItemTemplate and write headings for these repeated items using HeaderTemplate. You can display something at the bottom by using FooterTemplate. AlternativeItemTemplate and SeperatorTemplate are used in repeater for its layout. 
You can find Header and Footer template controls in your code file to display values on it.
  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Write code below in your aspx page.

        <asp:Repeater ID="Repeater1" runat="server">
               <asp:Label ID="Label1" runat="server" ></asp:Label>
                <asp:Label ID="Label2" runat="server" ></asp:Label>
                <asp:Label ID="Label3" runat="server" ></asp:Label>
            <td colspan="3">
                <asp:Label ID="Label4" runat="server" ></asp:Label>
    I am adding three Labels in HeaderTemplate and one Label in FooterTemplate. I will excess these Labels in code file and set values. 
  4. Write below function in your code behind file of your page. Include “System.Data” and “System.Data.SqlClient” namespaces.

    private DataTable ProductData() {
        SqlConnection con = new SqlConnection("Data Source=MyPC;Initial Catalog=C:\\SQL SERVER 2000 SAMPLE DATABASES\\NORTHWND.MDF;Integrated Security=True");
        string sql = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE UnitPrice < 10";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        return dt;
    Private Function ProductData() As DataTable
        Dim con As New SqlConnection("Data Source=MyPC;Initial Catalog=C:\SQL SERVER 2000 SAMPLE DATABASES\NORTHWND.MDF;Integrated Security=True")
        Dim sql As String = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE UnitPrice < 10"
        Dim cmd As New SqlCommand(sql, con)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        Return dt
    End Function
    First include required namespaces in your code file and then write a function to get data from database. If you want to use this function you must have NORTHWIND database and you need to set connection to your server. 
  5. Write below code in Page load method

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            Repeater1.DataSource = ProductData();
            Label label1 = Repeater1.Controls[0].FindControl("Label1") as Label;
            label1.Text = "Product ID";
            Label label2 = Repeater1.Controls[0].FindControl("Label2") as Label;
            label2.Text = "Product Name";
            Label label3 = Repeater1.Controls[0].FindControl("Label3") as Label;
            label3.Text = "Unit Price";
            Label label4 = Repeater1.Controls[Repeater1.Controls.Count - 1].FindControl("Label4") as Label;
            label4.Text = "
    Above products have unit price less than $10";
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Repeater1.DataSource = ProductData()
            Dim label1 As Label = TryCast(Repeater1.Controls(0).FindControl("Label1"), Label)
            label1.Text = "Product ID"
            Dim label2 As Label = TryCast(Repeater1.Controls(0).FindControl("Label2"), Label)
            label2.Text = "Product Name"
            Dim label3 As Label = TryCast(Repeater1.Controls(0).FindControl("Label3"), Label)
            label3.Text = "Unit Price"
            Dim label4 As Label = TryCast(Repeater1.Controls(Repeater1.Controls.Count - 1).FindControl("Label4"), Label)
            label4.Text = "<br />Above products have unit price less than $10"
        End If
    End Sub
    First I am binding ProductData() function to repeater control. Then I am getting Label controls from HeaderTemplate and seting their values. I am finding Label control from FooterTemplate and assigning it to Label4 in my code.
  6. View the Web Site in browser and see the result


Add Article Comment:
Name :
Email Address :
Comments :
<< How to pass multiple selected values of a GridView to another page in ASP.NET

Disclaimer - Privacy
© 2002-2017