Repeater Control in ASP.NET

The Repeater control is one of the Data Controls available in the ASP.Net Data Section of the Toolbox for displaying records in the front end. Now you may be wondering, since we have GridView, DataGrid and ListView controls in ASP.Net, why do we need a repeater control? Well let's consider a scenario where you are asked to create software to print tickets of any movie or a bill in a shopping mall. When you look at a ticket you can see that the header and footer section of all tickets that are printed (movie or any shopping mall bills) are nearly the same except with some change of values. And the middle of all tickets or bills vary depending on the seat being booked or the number of items being purchased in the shopping mall. For such type of scenarios we can use a Repeater control where we have to display a number of items repeated but with the same header and footer.

A Repeater control is a control that will not by default render itself like that of a GridView control (we normally call it an unformatted control that basically does not have any predefined layout), we explicitly have to define the layout of how the data has to be displayed to use a repeater control. That means we can customize the look and feel of how the data has to be displayed on the front end. It generally uses various templates for specifying the look of the control. The various types of templates are the same as for a GridView, DataGrid or ListView control in ASP.Net. Templates (such as HeaderTemplate, ItemTemplate, SeparatorTemplate, AlternatingItemTemplate, FooterTemplate) make up the repeater control.

How the Repeater Control works

The Repeater control, when bound to a datasource, will loop through all the records of the datasource and then generate the itemtemplate for that respective datasource record resulting in a new row being rendered. You can also perform some calculations when performing this looping by making use of the ItemBound Event of the Repeater Control.

How to Define the Layout Of a Repeater Control

The following describes how to define the layout of a Repeater Control:

  1. Well for defining layout, as we discussed earlier, we must use a Template that is provided by this control.
  2. We can define the layout as we choose by use of the HeaderTemplate for the header row of the control, then moving toward for displaying data we must use an itemtemplate or alternatingitemtemplate and
  3. In case we want any separation between two records we can always use a SeparaterTemplate.
  4. Finally we have to close the container tag in the footerTemplate that was opened in the headertemplate.

Out of the preceding template, HeaderTemplate and FooterTemplate are the template that will only be rendered once. And for every row every ItemTemplate, AlternatingItemTemplate and SeparatorTemplate will be rendered repeatedly.

Let us look at the following design source code for this:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">  
  4. <html xmlns="">  
  5. <head runat="server">  
  6.     <title></title>  
  7.     <style type="text/css">  
  8.         #even td  
  9.         {  
  10.             background-color: #9EEEF2;  
  11.         }  
  12.         tr, th  
  13.         {  
  14.             border: solid 1px lime;  
  15.         }  
  16.         #odd td  
  17.         {  
  18.             background-color: #066D7A;  
  19.         }  
  20.     </style>  
  21. </head>  
  22. <body>  
  23.     <form id="form1" runat="server">  
  24.     <div>  
  25.         <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">  
  26.             <HeaderTemplate>  
  27.                 <table style="background-color: White; font-family: Verdana; font-size: small;">  
  28.                     <tr style="background-color: #0D2D37; color: White;">  
  29.                         <th>  
  30.                             ID  
  31.                         </th>  
  32.                         <th>  
  33.                             Name  
  34.                         </th>  
  35.                         <th>  
  36.                             Address  
  37.                         </th>  
  38.                         <th>  
  39.                             Department  
  40.                         </th>  
  41.                         <th>  
  42.                             Salary  
  43.                         </th>  
  44.                     </tr>  
  45.             </HeaderTemplate>  
  46.             <ItemTemplate>  
  47.                 <tr id="even">  
  48.                     <td>  
  49.                         <asp:Label ID="lblID" runat="server" Text='<%#Bind("ID") %>'></asp:Label>  
  50.                     </td>  
  51.                     <td>  
  52.                         <asp:Label ID="Label1" runat="server" Text='<%#Bind("Name") %>'></asp:Label>  
  53.                     </td>  
  54.                     <td>  
  55.                         <asp:Label ID="Label2" runat="server" Text='<%#Bind("Address") %>'></asp:Label>  
  56.                     </td>  
  57.                     <td>  
  58.                         <asp:Label ID="Label3" runat="server" Text='<%#Bind("Department") %>'></asp:Label>  
  59.                     </td>  
  60.                     <td>  
  61.                         <asp:Label ID="Label4" runat="server" Text='<%#Bind("Salary") %>'></asp:Label>  
  62.                     </td>  
  63.                 </tr>  
  64.             </ItemTemplate>  
  65.             <AlternatingItemTemplate>  
  66.                 <tr id="odd">  
  67.                     <td>  
  68.                         <asp:Label ID="lblID" runat="server" Text='<%#Bind("ID") %>'></asp:Label>  
  69.                     </td>  
  70.                     <td>  
  71.                         <asp:Label ID="Label1" runat="server" Text='<%#Bind("Name") %>'></asp:Label>  
  72.                     </td>  
  73.                     <td>  
  74.                         <asp:Label ID="Label2" runat="server" Text='<%#Bind("Address") %>'></asp:Label>  
  75.                     </td>  
  76.                     <td>  
  77.                         <asp:Label ID="Label3" runat="server" Text='<%#Bind("Department") %>'></asp:Label>  
  78.                     </td>  
  79.                     <td>  
  80.                         <asp:Label ID="Label4" runat="server" Text='<%#Bind("Salary") %>'></asp:Label>  
  81.                     </td>  
  82.                 </tr>  
  83.             </AlternatingItemTemplate>  
  84.             <FooterTemplate>  
  85.                 </table>  
  86.             </FooterTemplate>  
  87.         </asp:Repeater>  
  88.     </div>  
  89.     </form>  
  90. </body>  
  91. </html>

The above code will render our data as in the following:

Image 1.jpg

In our example we perform some JavaScript functionality that basically tells us in an alert box about the data. For any of you not getting my point, this can best be understood by looking at the following diagram:

Image 2.jpg

Image 3.jpg

Here is the source code for it:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Web.UI.HtmlControls;  
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  13.     protected void Page_Load(object sender, EventArgs e)  
  14.     {  
  15.         if (!IsPostBack)  
  16.         {  
  17.             Repeater1.DataSource = LoadFile();  
  18.             Repeater1.DataBind();  
  19.         }  
  20.     }  
  21.     private DataTable LoadFile()  
  22.     {  
  23.         string filepath = Server.MapPath("~/EmpX.xml");  
  24.         DataSet ds = new DataSet();  
  25.         ds.ReadXml(filepath);  
  26.         DataColumn dc = new DataColumn("ViewDetails"typeof(string));  
  27.         dc.DefaultValue = "Open";  
  28.         ds.Tables[0].Columns.Add(dc);  
  30.         if (ds.Tables[0].Rows.Count > 0)  
  31.         {  
  32.             DataTable dt = ds.Tables[0];  
  33.             return dt;  
  34.         }  
  35.         else  
  36.             return null;  
  37.     }  
  38.     protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)  
  39.     {  
  40.         if (e.Item.ItemType == ListItemType.Item)  
  41.         {  
  42.             LinkButton btn = (LinkButton)e.Item.FindControl("lnkButton");  
  43.             btn.ForeColor = System.Drawing.Color.Orchid;  
  44.             btn.OnClientClick = "javascript:return displayAlert('" + (e.Item.ItemIndex + 1) + "');";  
  45.         }  
  46.         else if (e.Item.ItemType == ListItemType.AlternatingItem)  
  47.         {  
  48.             LinkButton btn = (LinkButton)e.Item.FindControl("lnkButton");  
  49.             btn.ForeColor = System.Drawing.Color.White;  
  50.             btn.OnClientClick = "javascript:return displayAlert('" + (e.Item.ItemIndex + 1) + "');";  
  51.         }  
  52.     }  
  53. }

Here we are just displaying the records as it is through JavaScript, but you can perform many more things such as deleting the appropriate record when the linkbutton is clicked, or redirecting the user to the specific page and many more things.

Hope you may find this article interesting and it may help you in your project.