Repeater Class
A data-bound list control that allows custom layout by repeating a specified template for each item displayed in the list.
Assembly: System.Web (in System.Web.dll)
'Declaration <AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _ Public Class Repeater _ Inherits Control _ Implements INamingContainer 'Usage Dim instance As Repeater
<asp:Repeater />
The Repeater control is a basic templated data-bound list. It has no built-in layout or styles, so you must explicitly declare all layout, formatting, and style tags within the control's templates.
The Repeater control is the only Web control that allows you to split markup tags across the templates. To create a table using templates, include the begin table tag (<table>) in the HeaderTemplate, a single table row tag (<tr>) in the ItemTemplate, and the end table tag (</table>) in the FooterTemplate.
The Repeater control has no built-in selection capabilities or editing support. You can use the ItemCommand event to process control events that are raised from the templates to the control.
The Repeater control provides two properties to support data binding. To bind data to any object that implements the System.Collections.IEnumerable interface (such as a System.Data.DataView, a System.Collections.ArrayList, a System.Collections.Hashtable, or an array), or the IListSource interface, use the DataSource property to specify the data source. When you set the DataSource property, you must manually write the code to perform data binding. To automatically bind the Repeater control to a data source represented by a data source control, set the DataSourceID property to the ID of the data source control to use. When you set the DataSourceID property, the Repeater control automatically binds to the specified data source control on the first request. Therefore, you do not need to explicitly call the DataBind method unless you have changed data-related properties of the Repeater control.
A Repeater control binds its ItemTemplate and AlternatingItemTemplate to either the data model declared and referenced by its DataSource property or the data source control specified by its DataSourceID property. The HeaderTemplate, FooterTemplate, and SeparatorTemplate are not data-bound.
If the Repeater control's data source is set but no data is returned, the control renders the HeaderTemplate and FooterTemplate with no items. If the data source is Nothing, the Repeater is not rendered.
At a minimum, every Repeater control must define an ItemTemplate. However, other optional templates described in the following table can be used to customize the appearance of the list.
Template name | Description |
---|---|
Defines the content and layout of items within the list. This template is required. | |
If defined, determines the content and layout of alternating (zero-based odd-indexed) items. If not defined, ItemTemplate is used. | |
If defined, is rendered between items (and alternating items). If not defined, a separator is not rendered. | |
If defined, determines the content and layout of the list header. If not defined, a header is not rendered. | |
If defined, determines the content and layout of the list footer. If not defined, a footer is not rendered. |
![]() |
---|
This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. ASP.NET provides an input request validation feature to block script and HTML in user input. Validation server controls are also provided to assess user input. For more information, see Validation Server Control Syntax. |
The following code example demonstrates how to use two simple Repeater controls on a page. The DataSource property is used to specify the data source for the Repeater control. The first Repeater displays its items in a table; the second Repeater displays its items in a comma-separated list.
<%@ Page Language="VB" AutoEventWireup="True" %> <!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> <title>Repeater Example</title> <script language="VB" runat="server"> Sub Page_Load(Sender As Object, e As EventArgs) If Not IsPostBack Then Dim values As New ArrayList() values.Add(New PositionData("Microsoft", "Msft")) values.Add(New PositionData("Intel", "Intc")) values.Add(New PositionData("Dell", "Dell")) Repeater1.DataSource = values Repeater1.DataBind() Repeater2.DataSource = values Repeater2.DataBind() End If End Sub Public Class PositionData Private myName As String Private myTicker As String Public Sub New(newName As String, newTicker As String) Me.myName = newName Me.myTicker = newTicker End Sub Public ReadOnly Property Name() As String Get Return myName End Get End Property Public ReadOnly Property Ticker() As String Get Return myTicker End Get End Property End Class </script> </head> <body> <h3>Repeater Example</h3> <form id="form1" runat="server"> <b>Repeater1:</b> <br /> <asp:Repeater id="Repeater1" runat="server"> <HeaderTemplate> <table border="1"> <tr> <td><b>Company</b></td> <td><b>Symbol</b></td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# DataBinder.Eval(Container.DataItem, "Name") %> </td> <td> <%# DataBinder.Eval(Container.DataItem, "Ticker") %> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <br /> <b>Repeater2:</b> <br /> <asp:Repeater id="Repeater2" runat="server"> <HeaderTemplate> Company data: </HeaderTemplate> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Name") %> (<%# DataBinder.Eval(Container.DataItem, "Ticker") %>) </ItemTemplate> <SeparatorTemplate>, </SeparatorTemplate> </asp:Repeater> </form> </body> </html>
The following code example demonstrates how to use the DataSourceID property to specify the data source for a Repeater control. The DataSourceID property is set to the ID property of the SqlDataSource control used to retrieve the data. When the page is loaded, the Repeater control automatically binds to the data source specified by the SqlDataSource control and the data is displayed to the user.
<%@ 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> <title>Repeater.DataSourceID Property Example</title> </head> <body> <form id="Form1" runat="server"> <h3>Repeater.DataSourceID Property Example</h3> <asp:repeater id="Repeater1" datasourceid="SqlDataSource1" runat="server"> <headertemplate> <table border="1"> <tr> <td><b>Product ID</b></td> <td><b>Product Name</b></td> </tr> </headertemplate> <itemtemplate> <tr> <td> <%# Eval("ProductID") %> </td> <td> <%# Eval("ProductName") %> </td> </tr> </itemtemplate> <footertemplate> </table> </footertemplate> </asp:repeater> <asp:sqldatasource id="SqlDataSource1" connectionstring="<%$ ConnectionStrings:NorthWindConnection%>" selectcommand="SELECT ProductID, ProductName FROM [Products] Where ProductID <= 10" runat="server"> </asp:sqldatasource> </form> </body> </html>
- AspNetHostingPermission
for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission
for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.