Export (0) Print
Expand All
15 out of 21 rated this helpful - Rate this topic

Repeater Class

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

For a list of all members of this type, see Repeater Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.Repeater

[Visual Basic]
Public Class Repeater
   Inherits Control
   Implements INamingContainer
[C#]
public class Repeater : Control, INamingContainer
[C++]
public __gc class Repeater : public Control, INamingContainer
[JScript]
public class Repeater extends Control implements INamingContainer

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

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

The Repeater is the only control that allows the developers to split HTML 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 has no built-in selection or editing support. The user may use the ItemCommand event to process control events that are raised from the templates to the control.

A Repeater binds its ItemTemplate and AlternatingItemTemplate to a data model declared and referenced by its DataSource property. The HeaderTemplate, FooterTemplate, and SeparatorTemplate are not data-bound.

If the data source of the Repeater is set but no data is returned, the control renders the HeaderTemplate and FooterTemplate with no items. If the data source is a null reference (Nothing in Visual Basic), the Repeater is not rendered.

At a minimum, every Repeater 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
ItemTemplate Defines the content and layout of items within the list. This template is required.
AlternatingItemTemplate If defined, this determines the content and layout of alternating (zero-based odd-indexed) items. If not defined, ItemTemplate is used.
SeparatorTemplate If defined, this is rendered between items (and alternating items). If not defined, a separator is not rendered.
HeaderTemplate If defined, this determines the content and layout of the list header. If not defined, a header is not rendered.
FooterTemplate If defined, this determines the content and layout of the list footer. If not defined, a footer is not rendered.
CAUTION   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 Controls.

Example

[Visual Basic, C#] The following example demonstrates the use of some simple Repeater controls. The first Repeater displays its items in a table; the second Repeater displays its items in a comma-separated list.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
 <head>
    <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 runat=server>
 
       <b>Repeater1:</b>
         
       <p>
         
       <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>
       <p>
         
       <b>Repeater2:</b>
       <p>
       <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>
 

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
 <head>
    <script language="C#" runat="server">
       void Page_Load(Object Sender, EventArgs e) {
          if (!IsPostBack) {
             ArrayList values = 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();
          }
       }
 
       public class PositionData {
         
          private string name;
          private string ticker;
 
          public PositionData(string name, string ticker) {
             this.name = name;
             this.ticker = ticker;
          }
 
          public string Name {
             get {
                return name;
             }
          }
 
          public string Ticker {
             get {
                return ticker;
             }
          }
       }
 
    </script>
 
 </head>
 <body>
 
    <h3>Repeater Example</h3>
 
    <form runat=server>
 
       <b>Repeater1:</b>
         
       <p>
         
       <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>
       <p>
         
       <b>Repeater2:</b>
       <p>
       <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>
 

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.WebControls

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

Repeater Members | System.Web.UI.WebControls Namespace | DataGrid | DataList

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.