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

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

Author: Rudy Pinson
Download Source Code : 1369_FindControlsInRepeater.zip

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.

    <table>
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
            <tr>
            <td>
               <asp:Label ID="Label1" runat="server" ></asp:Label>
            </td>
            <td>
                <asp:Label ID="Label2" runat="server" ></asp:Label>
            </td>
            <td>
                <asp:Label ID="Label3" runat="server" ></asp:Label>
            </td>
            </tr>
            </HeaderTemplate>
            <ItemTemplate>
            <tr>
            <td>
                <%#DataBinder.Eval(Container,"DataItem.ProductID")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container,"DataItem.ProductName")%>
            </td>
            <td>
                <%#DataBinder.Eval(Container,"DataItem.UnitPrice")%>
            </td>
        </tr>
        </ItemTemplate>
        <FooterTemplate>
        <tr>
            <td colspan="3">
                <asp:Label ID="Label4" runat="server" ></asp:Label>
            </td>
        </tr>
        </FooterTemplate>
        </asp:Repeater>
    </table>
     
    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.

    C#
     
    private DataTable ProductData() {
        SqlConnection con = new SqlConnection("Data Source=MyPC;Initial Catalog=C:\\SQL SERVER 2000 SAMPLE DATABASES\\NORTHWND.MDF;Integrated Security=True");
        con.Open();
        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();
        da.Fill(dt);
        con.Close();
        return dt;
    }
     
    VB.NET
     
    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")
        con.Open()
        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()
        da.Fill(dt)
        con.Close()
        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

    C#
     
    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            Repeater1.DataSource = ProductData();
            Repeater1.DataBind();
     
            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";
        }
    }
     
    VB.NET
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Repeater1.DataSource = ProductData()
            Repeater1.DataBind()
     
            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 DevASP.net