Repeater Controls in ASP.NET

Repeater Controls in ASP.NET 

 
The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database table, an XML file, or another list of items.
 
Repeater is a Data Bind Control. Data Bind 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. ASP .Net provides rich and wide variety of controls, which can be bound to the data. 
 
Repeater has 5 inline template to format it:
  1. <HeaderTemplate>
  2. <FooterTemplate>
  3. <ItemTemplate> 
  4. <AlternatingItemTemplate>
  5. <SeperatorTemplate>
  6. <AlternatingItemTemplate> 
HeaderTemplate: This template is used for elements that you want to render once before your ItemTemplate section.
 
FooterTemplate: - This template is used for elements that you want to render once after your ItemTemplate section.
 
ItemTemplate: This template is used for elements that are rendered once per row of data. It is used to display records
 
AlternatingItemTemplate: This template is used for elements that are rendered every second row of data. This allows you to alternate background colors. It works on even number of records only.
 
SeperatorTemplate: It is used for elements to render between each row, such as line breaks.
 
Some point about Repeater Control
  • It is used to display backend result set. It is used to display multiple tuple.
  • It is an unformatted control. The Repeater control is a basic templated data-bound list. It has no built-in layout or styles, so you must explicitly declare all layout, formatting, and style tags within the control's templates.
  • The Repeater control is the only Web control that allows you to split markup 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 control has no built-in selection capabilities or editing support. You can use the ItemCommand event to process control events that are raised from the templates to the control.
We have to use scriptlets for data access.
 
Data Bind Control can display connected and disconnected model.
 
Data Bind Control have DataBind() method and DataBound event
 
-> DataBind()
-> ItemCreated -> Event
-> DataBound -> Event
 
System.Commom.Data        namespace
-> DBDataRecord               Class
 
Controls -> Child Control
 
Every DataBindControl implement collection.
 
The data in DataSource is bound to Repeater using its DataBind Method. Once the data is bound, the format of each data item is defined by a template like ItemTemplate.
 

How to use a Repeater Control?

 
Create a new website with name RepeaterControl
 
Double click the web.config file and write following code in it:
  1. <connectionStrings>  
  2.     <add name="constr" connectionString="initial catalog=puran; data source=MCN002; integrated security=sspi"/>  
  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 bound it to a data source through its DataSource property. q
 
The output of the above program
 
repeater.gif 
 
This article is for making a beginner learn and use Repeater Control.
 
Note: In the above example you have to change the connection string according to your database and data source.
 

Conclusion 

 
I hope that this article would have helped you in understanding the Repeater Control in ASP.NET. Please share it if you know more about this article. Your feedback and constructive contributions are welcome.


Similar Articles