Wednesday, July 09, 2008

ASP: Repeater control

The Repeater control is a data-bound list control that allows custom layout by repeating a specified template for each item displayed in the list.
Typcially, Repeater control will have the following declaration:


<asp:Repeater ID="Repeater1" runat="server">
  <HeaderTemplate>
    ...
  </HeaderTemplate>
  <ItemTemplate>
    ...
  </ItemTemplate>
  <FooterTemplate>
    ...
  </FooterTemplate>


where the items inside the HeaderTemplate and the FooterTemplate will appear once, while the items in the ItemTemplate will repeat many times.

For example:


<asp:Repeater ID="Repeater1" runat="server">
  <HeaderTemplate>
    <table border="1" width="100%">
      <tr>
        <th> Title </th>
        <th> Artist </th>
        <th> Year </th>
      </tr>
  </HeaderTemplate>
  <ItemTemplate>
      <tr>
        <td> <%# DataBinder.Eval(Container.DataItem, "title") %> </td>
        <td> <%# DataBinder.Eval(Container.DataItem, "artist")%> </td>
        <td> <%# DataBinder.Eval(Container.DataItem, "year")%> </td>
      </tr>
  </ItemTemplate>
  <FooterTemplate>
    </table>
  </FooterTemplate>
</asp:Repeater>


There is also the AlternatingItemTemplate and the Separator template. Read more on the MSDN help pages

No comments: