ASP.NET Program to use Repeater control templates

Demonstrate the use of following templates of Repeater control:

  • HeaderTemplate
  • AlternatingItemTemplate
  • Itemtemplate
  • SeperatorTemplate
  • FooterTemplate
Answer:


Repeater:
The Repeater control displays the data items in a repeating list. Every repeater must define an ItemTemplate.

Template NameDescription
ItemTemplateIt is the required field of the repeater. It defines the content and layout of the items.
AlternatingItemTemplateIt determines the content and layout of  alternative items. If not defined, it uses ItemTemplate.
SeparatorTemplateThe SeparatorTemplate is rendered between items (and alternating items). If not defined, a separator is not rendered.
HeaderTemplateThe SeparatorTemplate is rendered between items (and alternating items). If not defined, a separator is not rendered.
HeaderTemplateThe HeaderTemplate determines the content and layout of the list header. If not defined, header is not rendered.
FooterTemplateThe FooterTemplate determines the content and layout of the list footer. If not defined, footer is not rendered.

Here is an example which demonstrates all templates available in Repeater control.

addRepeater.aspx

<html>
  <head>
    <script language="VB" runat="server">

        Sub Page_Load(sender As Object, e As EventArgs)
            If Not IsPostBack Then
                Dim values As ArrayList = New ArrayList()

                values.Add(New PositionData("Sachin ", "Cricket"))
                values.Add(New PositionData("Saina ", "Badminton"))
                values.Add(New PositionData("Mary Kom", " Boxing"))

                Repeater1.DataSource = values
                Repeater1.DataBind()

                Repeater2.DataSource = values
                Repeater2.DataBind()
            End If
        End Sub

        Class PositionData
            Dim m_name As String
            Dim m_sport As String
            Public Sub New(name As String, sport As String)
                MyBase.New
                m_name = name
                m_sport = sport
            End Sub

            ReadOnly Property Name As String
                Get
                    Return m_name
                End Get
            End Property

            ReadOnly Property sport As String
                Get
                    Return m_sport
                End Get
            End Property

        End Class

     </script>
  </head>
<body>
    <h3><font face="Verdana">Repeater Example</font></h3>
    <form runat=server>
        <b>Repeater1:</b>
        </p>
        <p>
        <asp:Repeater id=Repeater1 runat="server">
            <HeaderTemplate>
                <table border=1>
                  <tr>
                    <td><b>Name</b></td>
                    <td><b>Sport</b></td>
                  </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                  <td> <%# DataBinder.Eval(Container.DataItem, "Name") %> </td>
                  <td> <%# DataBinder.Eval(Container.DataItem, "sport") %> </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
        </p>
        <p>
        <b>Repeater2:</b>
        </p>
        <p>
        <asp:Repeater id=Repeater2 runat="server">
            <HeaderTemplate>
               <b>Name & Sport:</b>   
            </HeaderTemplate>
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "Name") %><br /> (<%# DataBinder.Eval(Container.DataItem, "sport") %>)
            </ItemTemplate>
            <SeparatorTemplate>, </SeparatorTemplate>
        </asp:Repeater>
       </p>
    </form>
</body>
</html>


Output:

repeater control