asp.net.ph

Skip Navigation Links

Adding Repeater Controls to a Web Forms Page

Controls You Can Use on Web Forms   ASP.NET Data Controls   Repeater Control


Essentially, to create a working Repeater control, you need to add the control to the page and bind the control to a data source.

The below procedures show the minimum steps needed to display data in a Repeater control.

To add a Repeater control to a Web Forms page

This first example shows a Repeater control that is bound to a SqlDataSource control.

  1. Declare an <asp:repeater> element on the page. For syntax, see Repeater Control Syntax.
  2. Specify the control’s DataSourceID property.
  3. Optionally set the control’s base properties.
    <asp:repeater id="myRepeater" 
       datasourceid="customers"
       width="90%"
       runat="server" >
  4. Within the Repeater declaration, declare an <ItemTemplate> element.
  5. Within the ItemTemplate, define each field from the data source that you intend to present.

    To display the value of a field in the ItemTemplate, use DataBinding Expression Syntax.

    <itemtemplate>
       <b>Product Code:</b> <%# Eval ( "ProductCode" ) %>
       <br>
       ...
       <b>Price:</b> <%# Eval ( "UnitPrice", "{0:c}" ) %>
    </itemtemplate>

NOTE: The ItemTemplate must contain at least one databinding expression that binds a control to a field from the data source; otherwise the Repeater has nothing to render.

The DataSource property of a Repeater can also be set programmatically, just like any Web server control property. Below shows an example of binding a data source to a Repeater in code.

<script language="C#" runat="server">
   void Page_Load ( object src, EventArgs e ) {
      string query = "SELECT * FROM pubs_Titles";
      myRepeater.DataSource = fetchReader ( query, "pubs" );
      myRepeater.DataBind ( );
   }
</script>
Repeater Example
Run Sample | View Source

The above examples apply several concepts and methods that are described elsewhere in this workshop. For particulars, see ADO.NET Primer, Introduction to Data Binding in Web Forms, Web Server Controls Templates.

See Also

Introduction to the Repeater Control   Responding to Events in Repeater Items



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note