Repeater control in ASP.NET 2.0


Repeater control:

The Repeater control is used to display the data items in representing list. The content and layout of list items in Repeater is defined using Templates. Every Repeater must define an Item Template.

Template:

There are the following Templates used in repeater control.

  • AlternatingItemTemplate 
  • FooterTemplate 
  • HeaderTemplate 
  • ItemTemplate 
  • SeparatorTemplate

FooterTemplate: The FooterTemplate determines the content and layout of the list footer. It is used to specify an optional footer row.

For Example
:

<
FooterTemplate></table></FooterTemplate>

HeaderTemplate:
The HeaderTemplate determines the content and layout of the list header. It is used to specify an optional header row.

For Example
:

<
HeaderTemplate>
      <table border="1" width="100%">
          <tr>
         <th>Title</th>
         <th>Artist</th>
         <th>Country</th>
         <th>Company</th>
         <th>Price</th>
         <th>Year</th>
          </tr>
</HeaderTemplate>

ItemTemplate:
It defines the content and layout of items within the list.

For Example
:

<ItemTemplate>
      <tr>
          <td><%# DataBinder.Eval(Container.DataItem, "title")%></td>
          <td><%# DataBinder.Eval(Container.DataItem, "artist")%></td>
          <td><%# DataBinder.Eval(Container.DataItem, "country")%></td>
          <td><%# DataBinder.Eval(Container.DataItem, "company")%></td>
          <td><%# DataBinder.Eval(Container.DataItem, "price")%></td>
          <td><%# DataBinder.Eval(Container.DataItem, "year")%></td>
      </tr>
</ItemTemplate>

AlternatingItemTemplate: It is used as follows:

For Example:

<AlternatingItemTemplate>
      <tr bgcolor=" #ffccff ">
          <td><%# DataBinder.Eval(Container.DataItem, "title")%></td>
          <td><%# DataBinder.Eval(Container.DataItem, "artist")%></td>
          <td><%# DataBinder.Eval(Container.DataItem, "country")%></td>
          <td><%# DataBinder.Eval(Container.DataItem, "company")%></td>
          <td><%# DataBinder.Eval(Container.DataItem, "price")%></td>
          <td><%# DataBinder.Eval(Container.DataItem, "year")%></td>
      </tr>
</AlternatingItemTemplate> 

SeparatorTemplate:
The SeparatorTemplate is rendered between items (and alternating items).

Repeater has no built-in layout or styles. You must explicitly declare all HTML layout, formatting, and style tags within the templates of the control.

In
the following section you will learn how you can access data into Repeater control from XMLFile.

For Example
:

To create a list within an HTML table, you might declare the <table> tag in the HeaderTemplate, a table row in the ItemTemplate, and the </table> tag in the FooterTemplate as follows-

Default.aspx file:

<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeter.aspx.cs" Inherits="sapna_Repeter" %>
<!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 runat="server">
    <title>Untitled Page</title>
</
head>
<body>
    <form id="form1" runat="server">
        &nbsp;<asp:Repeater ID="cdcatalog" runat="server">
            <HeaderTemplate>
                <table border="1" width="100%">
                    <tr>
                        <th>Title</th>
                        <th>Artist</th>
                        <th>Country</th>
                        <th>Company</th>
                        <th>Price</th>
                        <th>Year</th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><%# DataBinder.Eval(Container.DataItem, "title")%></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "artist")%></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "country")%></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "company")%></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "price")%></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "year")%></td>
                </tr>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <tr bgcolor=" #ffccff ">
                    <td><%# DataBinder.Eval(Container.DataItem, "title")%></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "artist")%></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "country")%></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "company")%></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "price")%></td>
                    <td><%# DataBinder.Eval(Container.DataItem, "year")%></td>
                </tr>
            </AlternatingItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </form>
</body>
</html>

Default.aspx.cs:

using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class sapna_Repeter : System.Web.UI.Page
{
   
DataSet mycdcatalog = new DataSet();
   
protected void Page_Load(object sender, EventArgs e)
    {
        mycdcatalog.ReadXml(Server.MapPath(
"../sapna/XMLFile.xml"));
        cdcatalog.DataSource = mycdcatalog;
        cdcatalog.DataBind();
    }
}

Output:

Title

Artist

Country

Company

Price

Year

Empire Burlesque

Bob Dylan

INDIA

MCN Solutions

10.90

1991

Hide your heart

Bonnie Tyler

UK

CBS Records

9.90

1988

Greatest Hits

Lucy

USA

RCA

9.90

1982

Still got the blues

Partap

UK

Virgin records

10.20

1990

Eros

Sapna

EU

BMG

9.90

1997


Similar Articles