asp.net.ph

Skip Navigation Links

Repeater Control Syntax

ASP.NET Syntax   ASP.NET Syntax for Web Controls


A data-bound list control that allows custom layout by repeating a specified template for each item displayed in the list.

Declarative Syntax

For information on the individual members of this class, see Repeater in the class library.

Remarks

The Repeater control renders a basic templated data-bound list. The Repeater control has no built-in layout or styles; you must explicitly declare all HTML layout, formatting, and style tags within the control’s templates.

The following table lists the different templates of the Repeater control.

Template Description
HeaderTemplate Defines the layout of elements to render once, before any data-bound rows have been rendered. A typical use is to begin a container element, such as a <table>.
ItemTemplate Defines the layout of elements that are rendered once for each row in the data source. To display data in the ItemTemplate, declare one or more server controls and set their data-binding expressions to evaluate to a field in the Repeater control’s DataSource.
AlternatingItemTemplate Like the ItemTemplate, but rendered for every other row in the Repeater control. You can specify a different appearance for the AlternatingItemTemplate element by setting its style properties.
FooterTemplate Defines the layout of elements to render once, after all data-bound rows have been rendered. A typical use is to close an element opened in the HeaderTemplate item, such as a </table>.
SeparatorTemplate Defines the layout of elements to render between each row, typically line breaks ( <br> tags ), horizontal lines ( <hr> tags ), and so on.

NOTE: You cannot bind controls in the header, footer, and separator templates using design-time databinding expressions. You can, though, bind controls in these templates programmatically.

The Repeater control has no built-in selection or editing support. You can, however, define a handler for the control’s ItemCommand event to process control events that are sent from the templates to the control.

The control binds its Item and AlternatingItem templates to a data structure referenced in the control’s DataSource property. If the DataSource property is not set, the Repeater control is not rendered. If the DataSource is set but no data is returned, the control renders the Header and Footer templates, with no items.

Syntax Example

The Repeater control is different from other data list controls in that it allows you to place HTML fragments in its templates. This allows you to create a complex HTML structure, such as a table.

The following example illustrates use of a Repeater control.

repeater1.aspx
Run Sample | View Source

The sample is implemented basically as follows:

  1. Start the table by declaring the opening <table> tag in the HeaderTemplate. In this example, we add a table header row <tr> containing <th> elements to display the headings for each field in the data source, although this is optional.
  2. Next, in the ItemTemplate, declare a single table row <tr> containing <td> elements to display the values for each field in the data source. At run time, the ItemTemplate is rendered once for each row in the data source.
  3. If you want a different appearance for alternating items in the table, define an AlternatingItemTemplate with the same contents as the ItemTemplate, except with a different style specified.
  4. Finally, complete the table by declaring the closing </table> tag in the FooterTemplate.
<body>

<asp:repeater id="myRepeater" runat="server">

   <headertemplate>
      <table width = "100%" cellspacing=1 cellpadding=3 style = "font:9pt verdana">
      <tr style = "background-color:#ddc">
         <th>Title</th>
         <th>Title ID</th>
         <th>Type</th>
         <th>Publisher ID</th>
         <th>Price</th></tr>
   </headertemplate>

   <itemtemplate>
      <tr style = "background-color:whitesmoke; height:17pt">
         <td><%# DataBinder.Eval ( Container.DataItem, "title" ) %></td>
         <td><%# DataBinder.Eval ( Container.DataItem, "title_id" ) %></td>
         <td><%# DataBinder.Eval ( Container.DataItem, "type" ) %></td>
         <td><%# DataBinder.Eval ( Container.DataItem, "pub_id" ) %></td>
         <td><%# DataBinder.Eval ( Container.DataItem, "price", "{0:c2}" ) %></td></tr>
   </itemtemplate>

   <footertemplate>
      </table>
   </footertemplate>

</asp:repeater>

</body>
See Also

Repeater Class   Repeater Web Server Control



© 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