Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to: Add a Repeater Control to a Web Forms Page

The Repeater Web server control is a container control that allows you to create custom lists out of any data that is available to the page. Adding a Repeater Web server control to a page requires several steps. The following procedure describes the minimum that you must do to create a working Repeater control.

To add a Repeater Web server control to an ASP.NET page

  1. Add a data source control to the page, such as a SqlDataSource or ObjectDataSource control. For details, see Data Source Web Server Controls.

  2. Configure the data source control to perform a query.

    If you are using a SqlDataSource or AccessDataSource control, specify connection information and an SQL query for the SelectCommand property. If you are using a ObjectDataSource control, configure its SelectMethod property.

  3. Add an <asp:Repeater> element to the page. For syntax, see Repeater Web Server Control Declarative Syntax.

  4. Set the Repeater control's DataSourceID property to the ID of the data source control.

  5. Add an <ItemTemplate> element into the page as a child of the Repeater control. For syntax, see Repeater Web Server Control Declarative Syntax.

    The Repeater control must contain at least an ItemTemplate that in turn contains data-bound controls in order for the control to render at run time.


    For background information about using templates, see ASP.NET Web Server Controls Templates.

  6. Add HTML markup and Web server controls or HTML server controls to the ItemTemplate.

  7. Bind the child controls to data from the query using the Eval data-binding function, as shown in the following example.

        <td bgcolor="#CCFFCC">
          <asp:Label runat="server" ID="Label1" 
                     Text='<%# Eval("CategoryName") %>' />
        <td bgcolor="#CCFFCC">
          <asp:Label runat="server" ID="Label2" 
                     Text='<%# Eval("Description") %>' />

    For information on data-binding functions, see Data Binding Expression Syntax.

  8. Optionally, define an AlternatingItemTemplate, SeparatorTemplate, HeaderTemplate, or FooterTemplate.


The following example shows how to use a Repeater control to display data in an HTML table. The table element begins in the HeaderTemplate and ends in the FooterTemplate. Within the body of the Repeater control, table cells are used to display columns from the data source. The AlternatingItemTemplate element is identical to the ItemTemplate item except that the background color for the table cells is different to create a banded effect.

<%@ Page Language="C#" %>
<head runat="server">
    <title>ASP.NET Repeater Example</title>
    <form id="form1" runat="server">
      <asp:Repeater ID="Repeater1" runat="server" 

              <td bgcolor="#CCFFCC">
                <asp:Label runat="server" ID="Label1" 
                    text='<%# Eval("CategoryName") %>' />
              <td bgcolor="#CCFFCC">
                  <asp:Label runat="server" ID="Label2" 
                      text='<%# Eval("Description") %>' />

              <td >
                <asp:Label runat="server" ID="Label3" 
                    text='<%# Eval("CategoryName") %>' />
              <td >
                 <asp:Label runat="server" ID="Label4" 
                     text='<%# Eval("Description") %>' />


              "<%$ ConnectionStrings:NorthwindConnectionString %>"
          ID="SqlDataSource1" runat="server" 
          SelectCommand="SELECT [CategoryID], [CategoryName], 
              [Description] FROM [Categories]">

The example requires a connection string that can be used to connect to the sample Northwind database in Microsoft SQL Server. The connection string must be defined in the <connectionStrings> element of the application's Web.config file. The <connectionStrings> section might look like the following example:

      connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;" />

      <!-- Other configuration settings -->s

See Also

Community Additions

© 2015 Microsoft