The
Because the Repeater control has no default look, you can use it to create many kinds of lists, including the following:
-
A table layout
-
A comma-delimited list (for example, a, b, c, d, and so on)
-
An XML formatted list
Using Templates with the Repeater Control
To use the Repeater control, you create templates that define the layout of the control's content. Templates can contain any combination of markup and controls. If no templates are defined, or if none of the templates contain elements, the control does not appear on the page when the application is run.
The following table describes the templates that are supported by the Repeater control.
Template property | Description |
---|---|
|
Contains the HTML elements and controls to render once for each data item in the data source. |
|
Contains the HTML elements and controls to render once for every other data item in the data source. Typically, this template is used to create a different look for the alternating items, such as a different background color than the color that is specified in the |
|
Contains the text and controls to render at the beginning and end of the list, respectively. |
|
Contains the elements to render between each item. A typical example might be a line (using an hr element). |
For more information, see ASP.NET Web Server Controls Templates and How to: Create ASP.NET Web Server Control Templates.
Binding Data to the Repeater Control
The Repeater control must be bound to a data source. The most common data source is a data source control, such as a
When binding data, you specify a data source for the Repeater control as a whole. When you add controls to the Repeater control — for example, when you add
Visual BasicВ | Copy Code |
---|---|
<%@ Page Language="VB" %> <!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>ASP.NET Repeater Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> <HeaderTemplate> <table> <tr> <th> Name</th> <th> Description</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td bgcolor="#CCFFCC"> <asp:Label runat="server" ID="Label1" Text='<%# Eval("CategoryName") %>' /> </td> <td bgcolor="#CCFFCC"> <asp:Label runat="server" ID="Label2" Text='<%# Eval("Description") %>' /> </td> </tr> </ItemTemplate> <AlternatingItemTemplate> <tr> <td> <asp:Label runat="server" ID="Label3" Text='<%# Eval("CategoryName") %>' /> </td> <td> <asp:Label runat="server" ID="Label4" Text='<%# Eval("Description") %>' /> </td> </tr> </AlternatingItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" ID="SqlDataSource1" runat="server" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"></asp:SqlDataSource> </div> </form> </body> </html> |
C#В | Copy Code |
---|---|
<%@ Page Language="C#" %> <!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>ASP.NET Repeater Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> <HeaderTemplate> <table> <tr> <th> Name</th> <th> Description</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td bgcolor="#CCFFCC"> <asp:Label runat="server" ID="Label1" Text='<%# Eval("CategoryName") %>' /> </td> <td bgcolor="#CCFFCC"> <asp:Label runat="server" ID="Label2" Text='<%# Eval("Description") %>' /> </td> </tr> </ItemTemplate> <AlternatingItemTemplate> <tr> <td> <asp:Label runat="server" ID="Label3" Text='<%# Eval("CategoryName") %>' /> </td> <td> <asp:Label runat="server" ID="Label4" Text='<%# Eval("Description") %>' /> </td> </tr> </AlternatingItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" ID="SqlDataSource1" runat="server" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"></asp:SqlDataSource> </div> </form> </body> </html> |
Note |
---|
You cannot bind controls in the header, footer, and separator templates using the Eval data binding function. If you have controls in these templates, you can simply define their properties statically. |
For an overview of data binding in Web server controls, see
Events Supported by the Repeater Control
The Repeater control supports several events. One of them, the
The
For more information, see How to: Respond to Button Events in DataList, Repeater, or GridView Items.