Understanding the Repeater Control in ASP.NET

As we all know to display data on a web application is important as per performance perspective. There are several data controls available in ASP.NET like GridView, DetailsView, FormView, Repeater, etc. But between all of these the Repeaters are much faster than others.

A Repeater Control is used to show the data as repeated mode. Data can bound from any type of data source like database, xml, etc.

It has several properties to define the Repeater control data but DataSource property is used to bind the data with Repeater. It provides different kinds of the templates for displaying the data. It doesn’t provide the auto code for paging, shorting, etc. You just need to write the manual code to achieve this.

ItemTemplate: It is used to render once per row of data.

AlternatingItemTemplate: It is used to render alternate row of data. You can change some styling with alternate row.

HeaderTemplate: It is used to render you header data which comes before your ItemTemplate data.

FooterTemplate: It is used to show details of that type of data which comes in the footer part. It always comes after the ItemTemplate data.

SeperatorTemplate: It is used to make separation between each line like you can place line breaks using this template.

Create an ASP.NETWeb Forms application “RepeaterControlDemo” using Visual Studio 2015 and add a new Web Form.

new

Provide the name for the page as Employee.aspx. Here we will add the Repeater control and bind the data from the database.

item

When you add new web form, the default template for the page as in the following.

web

Add Repeater Control

You can add Repeater using two ways, first you can direct drag and drop the Repeater Control from the ToolBox.

ToolBox

Or you can write code manually. I always prefer to write the whole code manually.

As I am going to bind the data with Repeater from the database, so you can modify the Employee.aspx page as in the following code,

Employee.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="RepeaterControlDemo.Employee" %>  
  2.     <!DOCTYPE html>  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div>  
  12.                 <asp:Repeater ID="rptEmployee" runat="server">  
  13.                     <HeaderTemplate>  
  14.                         <table border="1">  
  15.                             <tr>  
  16.                                 <th scope="col" style="width: 80px">Employee Id  
  17.                                 </th>  
  18.                                 <th scope="col" style="width: 120px">Name  
  19.                                 </th>  
  20.                                 <th scope="col" style="width: 100px">Email  
  21.                                 </th>  
  22.                                 <th scope="col" style="width: 120px">Age  
  23.                                 </th>  
  24.                                 <th scope="col" style="width: 100px">Address  
  25.                                 </th>  
  26.                                 <th scope="col" style="width: 100px">Department Name  
  27.                                 </th>  
  28.                             </tr>  
  29.                     </HeaderTemplate>  
  30.                     <ItemTemplate>  
  31.                         <tr>  
  32.                             <td>  
  33.                                 <asp:Label ID="lblEmployeeId" runat="server" Text='<%# Eval("Id") %>' />  
  34.                             </td>  
  35.                             <td>  
  36.                                 <asp:Label ID="lblEmployeeName" runat="server" Text='<%# Eval("Name") %>' />  
  37.                             </td>  
  38.                             <td>  
  39.                                 <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>' />  
  40.                             </td>  
  41.                             <td>  
  42.                                 <asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age") %>' />  
  43.                             </td>  
  44.                             <td>  
  45.                                 <asp:Label ID="lblAddress" runat="server" Text='<%# Eval("Address") %>' />  
  46.                             </td>  
  47.                             <td>  
  48.                                 <asp:Label ID="lblDepartment" runat="server" Text='<%# Eval("DepartmentName") %>' />  
  49.                             </td>  
  50.                         </tr>  
  51.                     </ItemTemplate>  
  52.                     <FooterTemplate>  
  53.                         </table>  
  54.                     </FooterTemplate>  
  55.                 </asp:Repeater>  
  56.             </div>  
  57.         </form>  
  58.     </body>  
  59.   
  60.     </html>  

Employee.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Web.UI;  
  9. using System.Web.UI.WebControls;  
  10. namespace RepeaterControlDemo   
  11. {  
  12.     public partial class Employee: System.Web.UI.Page  
  13.     {  
  14.         protected void Page_Load(object sender, EventArgs e)  
  15.         {  
  16.             if (!this.IsPostBack)   
  17.             {  
  18.                 BindEmployeeData();  
  19.             }  
  20.         }  
  21.         private void BindEmployeeData()   
  22.         {  
  23.             string connection = ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString;  
  24.             using(SqlConnection con = new SqlConnection(connection))   
  25.             {  
  26.                 using(SqlCommand objCommand = new SqlCommand("SELECT * FROM Employees emp INNER JOIN Department dep ON emp.Departmentid=dep.DepartmentId", con))   
  27.                 {  
  28.                     using(SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(objCommand))  
  29.                     {  
  30.                         DataTable dt = new DataTable();  
  31.                         dt.Columns.Add("Id");  
  32.                         dt.Columns.Add("Name");  
  33.                         dt.Columns.Add("Email");  
  34.                         dt.Columns.Add("Age");  
  35.                         dt.Columns.Add("Address");  
  36.                         dt.Columns.Add("DepartmentName");  
  37.                         objSqlDataAdapter.Fill(dt);  
  38.                         rptEmployee.DataSource = dt;  
  39.                         rptEmployee.DataBind();  
  40.                     }  
  41.                 }  
  42.             }  
  43.         }  
  44.     }  
  45. }  
Web.Config
  1. <connectionStrings>  
  2.     <add name="myconnection" connectionString="Data Source=My-Computer;Initial Catalog=TestEmployee;Integrated Security=True; User Id=sa; Password=mukeshkumar" providerName="System.Data.SqlClient" />  
  3. </connectionStrings>  
So, now you can run the project to see how repeater control displays your data.

displays

You can add the AlternatingItemTemplate and provide the different look and feel for alternate rows,
  1. <AlternatingItemTemplate>  
  2.     <tr style="background-color:#0094ff">  
  3.         <td>  
  4.             <asp:Label ID="lblEmployeeId" runat="server" Text='<%# Eval("Id") %>' />  
  5.         </td>  
  6.         <td>  
  7.             <asp:Label ID="lblEmployeeName" runat="server" Text='<%# Eval("Name") %>' />  
  8.         </td>  
  9.         <td>  
  10.             <asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>' />  
  11.         </td>  
  12.         <td>  
  13.             <asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age") %>' />  
  14.         </td>  
  15.         <td>  
  16.             <asp:Label ID="lblAddress" runat="server" Text='<%# Eval("Address") %>' />  
  17.         </td>  
  18.         <td>  
  19.             <asp:Label ID="lblDepartment" runat="server" Text='<%# Eval("DepartmentName") %>' />  
  20.         </td>  
  21.     </tr>  
  22. </AlternatingItemTemplate>  

emp
We can also add the HeaderTemplate and FooterTemplate to make it beautiful.

HeaderTemplate
  1. <HeaderTemplate>  
  2.     <table border="1">  
  3.         <tr>  
  4.             <th scope="col" style="width: 80px">Employee Id  
  5.             </th>  
  6.             <th scope="col" style="width: 120px">Name  
  7.             </th>  
  8.             <th scope="col" style="width: 100px">Email  
  9.             </th>  
  10.             <th scope="col" style="width: 120px">Age  
  11.             </th>  
  12.             <th scope="col" style="width: 100px">Address  
  13.             </th>  
  14.             <th scope="col" style="width: 100px">Department Name  
  15.             </th>  
  16.         </tr>  
  17. </HeaderTemplate>  
FooterTemplate
  1. <FooterTemplate>  
  2.     <tr>  
  3.         <td colspan="6">  
  4.             This is footer section  
  5.         </td>  
  6.     </tr>  
  7.     </table>  
  8. </FooterTemplate>  
Conclusion

So, today we learned about Repeater Control. How to bind data with Repeater control as well as we learned about different kinds of template in Repeater.

Suggest, what do you think?

Thanks for reading this article, hope you have enjoyed it. Please share your valuable feedback and suggestion through comments.


Similar Articles