Repeater Controls in ASP.Net

The Repeater control is used to display a repeated list of items that are bound to the control.

Repeater is a data bound control. Data bound controls are container controls. Data binding is the process of creating a link between the data source and the presentation UI to display the data.

The Repeater has the following 5 inline template to format it:



Data bound controls can display either a connected or a disconnected model.

Data bound controls have a DataBind() method and DataBound event as in the following:

  • DataBind()
  • ItemCreated -> Event
  • DataBound -> Event

System.Commom.Data       namespace

DBDataRecord                      Class

ControlsChild                       Control

How to use a Repeater Control

Create a new website with the name RepeaterControl.

Double-click the web.config file and write the following code in it:

  1. <connectionStrings>  
  2. <add name="constr" connectionString="initial catalog=Development-PC; data source=VilasDB; integrated security=true"/>  
  3.  </connectionStrings>  
Default.aspx file code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <link rel="Stylesheet" type="text/css" href="StyleSheet.css" />  
  6.     <title>Repeater Controls in ASP.NET</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <asp:Repeater ID="RepeatInformation" runat="server">  
  12.             <HeaderTemplate>  
  13.                 <table class="tblcolor">  
  14.                     <tr>  
  15.                         <b>  
  16.                             <td>  
  17.                                 Roll No  
  18.                             </td>  
  19.                             <td>  
  20.                                 Student Name  
  21.                             </td>  
  22.                             <td>  
  23.                                 Total Fees  
  24.                             </td>  
  25.                         </b>  
  26.                     </tr>  
  27.             </HeaderTemplate>  
  28.             <ItemTemplate>  
  29.                 <tr class="tblrowcolor">  
  30.                     <td>  
  31.                         <%#DataBinder.Eval(Container,"DataItem.RollNo")%>  
  32.                     </td>  
  33.                     <td>  
  34.                         <%#DataBinder.Eval(Container,"DataItem.Name")%>  
  35.                     </td>  
  36.                     <td>  
  37.                         <%#DataBinder.Eval(Container,"DataItem.Fees")%>  
  38.                     </td>  
  39.                 </tr>  
  40.             </ItemTemplate>  
  41.             <SeparatorTemplate>  
  42.                 <tr>  
  43.                     <td>  
  44.                         <hr />  
  45.                     </td>  
  46.                     <td>  
  47.                         <hr />  
  48.                     </td>  
  49.                     <td>  
  50.                         <hr />  
  51.                     </td>  
  52.                 </tr>  
  53.             </SeparatorTemplate>  
  54.             <AlternatingItemTemplate>  
  55.                 <tr>  
  56.                     <td>  
  57.                         <%#DataBinder.Eval(Container,"DataItem.RollNo")%>  
  58.                     </td>  
  59.                     <td>  
  60.                         <%#DataBinder.Eval(Container,"DataItem.Name")%>  
  61.                     </td>  
  62.                     <td>  
  63.                         <%#DataBinder.Eval(Container,"DataItem.Fees")%>  
  64.                     </td>  
  65.                 </tr>  
  66.             </AlternatingItemTemplate>  
  67.             <SeparatorTemplate>  
  68.                 <tr>  
  69.                     <td>  
  70.                         <hr />  
  71.                     </td>  
  72.                     <td>  
  73.                         <hr />  
  74.                     </td>  
  75.                     <td>  
  76.                         <hr />  
  77.                     </td>  
  78.                 </tr>  
  79.             </SeparatorTemplate>  
  80.             <FooterTemplate>  
  81.                 <tr>  
  82.                     <td>  
  83.                         School Records displayed  
  84.                     </td>  
  85.                 </tr>  
  86.                 </table>  
  87.             </FooterTemplate>  
  88.         </asp:Repeater>  
  89.     </div>  
  90.     </form>  
  91. </body>  
  92. </html>  
Code behind file code
  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12. using System.Data.SqlClient;  
  13.   
  14. public partial class _Default: System.Web.UI.Page   
  15. {  
  16.     SqlConnection con;  
  17.     SqlCommand cmd = new SqlCommand();  
  18.     protected void Page_Load(object sender, EventArgs e)   
  19.     {  
  20.         con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);  
  21.         cmd.Connection = con;  
  22.         cmd.CommandText = "select * from student";  
  23.         con.Open();  
  24.         RepeatInformation.DataSource = cmd.ExecuteReader();  
  25.         RepeatInformation.DataBind();  
  26.         con.Close();  
  27.     }  
  28. }  
The ASP.NET Repeater Control will not render the result unless you bind it to a data source through its DataSource property.

Example of a Repeater Control:
  • ID
  • Login
  • FirstName
  • LastName
  • DateAdded

FIRST STEP - INSERT THE REPEATER CONTROL ONTO THE PAGE

The Repeater control allows you to create templates. For example you can use a template to customize the layout of the individual rows.

  • HeaderTemplate

    Template for elements that you want to render once before your ItemTemplate section.

  • ItemTemplate

    Template for elements rendered once per row of data.

  • AlternatingItemTemplate

    Template for elements that are rendered every other row of data. This allows you to alternate background colors, for example.

  • SeparatorTemplate

    Template for elements to render between each row, such as line breaks.

  • FooterTemplate

    Template for elements that you want to render once after your ItemTemplate section.

ClientNetroStar.aspx

  1. <asp:Repeater ID="rptList" runat="server" OnItemDataBound="rptList_ItemDataBound">  
  2.     <HeaderTemplate>  
  3.         <table>  
  4.             <tr>  
  5.                 <th>  
  6.                     <asp:Label ID="Label14" Text="Login" runat="server"></asp:Label>  
  7.                 </th>  
  8.                 <th>  
  9.                     <asp:Label ID="Label1" Text="F_Name" runat="server"></asp:Label>  
  10.                 </th>  
  11.                 <th>  
  12.                     <asp:Label ID="Label2" Text="L_Name" runat="server"></asp:Label>  
  13.                 </th>  
  14.                 <th>  
  15.                     <asp:Label ID="Label3" Text="Date Added" runat="server"></asp:Label>  
  16.                 </th>  
  17.                 <th>  
  18.                     <asp:Label ID="Label6" Text="Delete" runat="server"></asp:Label>  
  19.                 </th>  
  20.             </tr>  
  21.         </HeaderTemplate>  
  22.         <ItemTemplate>  
  23.             <tr>  
  24.                 <td>  
  25.                     <asp:LinkButton ID="lbnLogin" Text='<%# Eval("Login") %>' runat="server"  
  26. OnClick="lnkClient_Click" CommandArgument='<%# Eval("ID").ToString() %>'>  
  27.                     </asp:LinkButton>  
  28.                 </td>  
  29.                 <td>  
  30.                     <asp:LinkButton ID="lbnFirstName" Text='<%# Eval("FirstName").ToString() %>'   
  31. runat="server"  
  32. OnClick="lnkClient_Click" CommandArgument='<%# Eval("ID").ToString() %>'>  
  33.                     </asp:LinkButton>  
  34.                 </td>  
  35.                 <td>  
  36.                     <asp:LinkButton ID="LinkButton2" Text='<%# Eval("LastName").ToString() %>'   
  37. runat="server"  
  38. OnClick="lnkClient_Click" CommandArgument='<%# Eval("ID").ToString() %>'>  
  39.                     </asp:LinkButton>  
  40.                 </td>  
  41.                 <td>  
  42.                     <asp:LinkButton ID="LinkButton1" Text='<%# Eval("DateAdded") %>' runat="server"  
  43. OnClick="lnkClient_Click" CommandArgument='<%# Eval("ID").ToString() %>'>  
  44.                     </asp:LinkButton>  
  45.                 </td>  
  46.                 <td>  
  47.                     <asp:LinkButton ID="lbnDelete" Text="delete" runat="server"  
  48. OnClick="lbnDelete_Click" CommandArgument='<%# Eval("ID").ToString() %>'>  
  49.                     </asp:LinkButton>  
  50.                 </td>  
  51.             </tr>  
  52.         </ItemTemplate>  
  53.         <FooterTemplate>  
  54.         </table>  
  55.     </FooterTemplate>  
  56. </asp:Repeater>  


Similar Articles