ASP.NET Repeater Templates

The Repeater control in ASP.NET provides developers with the flexibility to customize the appearance of the list by utilizing various optional templates. While the ItemTemplate is required and generates output for each data row, there are other templates available to further customize the list's appearance. These templates include AlternatingItemTemplate, SeparatorTemplate, HeaderTemplate, and FooterTemplate.

ItemTemplate

The ItemTemplate is a crucial template required by the Repeater control. It defines the layout and structure for each item in the list, producing one row of output for every data row. Developers can include HTML markup, controls, and data-binding expressions within the ItemTemplate to display the data from the data source.

repeater-template

The AlternatingItemTemplate is similar to the ItemTemplate but is rendered for every other data row. This template allows developers to apply a different visual style or formatting to alternate rows in the list, enhancing readability and visual appeal.

HeaderTemplate and FooterTemplate

The HeaderTemplate and FooterTemplate are used to render HTML immediately before and after all the data rows have been rendered, respectively. These templates are typically used to display summary information, column headings, or other content that should appear at the top or bottom of the list.

The SeparatorTemplate, as the name suggests, is responsible for rendering items between each data row. It allows developers to insert HTML tags such as BR, P, or HR to create visual separation between consecutive data rows.

The following ASP.NET program shows how to use Templates in Repeater Control.

Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater id="repeater1" runat="server" DataSourceID="SqlDataSource1"> <HeaderTemplate> <b><u>HEADER TEXT HERE</u></b><hr> </HeaderTemplate> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "stor_id")%>    <%# DataBinder.Eval(Container.DataItem, "stor_name")%> <br /> </ItemTemplate> <SeparatorTemplate> <hr> </SeparatorTemplate> <FooterTemplate> <hr><b><u>FOOTER TEXT HERE</u></b> </FooterTemplate> </asp:Repeater> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SQLDbConnection %>" SelectCommand="select [stor_id],[stor_name] from stores" /> </div> </form> </body> </html>

Conclusion

The Repeater control provides various optional templates, such as the AlternatingItemTemplate, SeparatorTemplate, HeaderTemplate, and FooterTemplate, in addition to the required ItemTemplate. These templates enable developers to customize the appearance of the list, add alternating styles, include headers and footers, and create visual separation between data rows.