Use Repeater Data Control In ASP.NET

The Repeater control is used to display items that are bound to the control. Repeater Control works like Grid View and other Data Controls. But the main difference is, that it doesn’t have any predefined structured or format like GridView.

How to use:

We have the following 5 template to use Repeater Control.

  • ItemTemplate: ItemTemplate defines how each item is rendered from data source collection.

  • AlternatingItemTemplate: AlternatingItemTemplates is used to change the background color and styles of AlternatingItems in DataSource collection.

  • HeaderTemplate: HeaderTemplate is used to display header text for DataSource collection and apply different styles for header text.

  • FooterTemplate: FooterTemplate is used to display footer element for DataSource collection.

  • SeparatorTemplate: SeparatorTemplate will determine separator element which separates each Item in Item collection.

Also, there are different methods used to bind the data with Repeater Controls.

  • Eval(): Eval is one-way, read only data binding.
  • Bind(): Bind is two-way, read/write data binding.

Example:

CRUD operations using Repeater Control.

Step 1: Create “Sql Helper” Class to establish a secure connection with DataBase.

  1. public class SqlHelper  
  2. {  
  3.     SqlConnection _con;  
  4.     SqlCommand _cmd;  
  5.     SqlDataAdapter _da;  
  6.     DataTable _dt;  
  7.     public SqlHelper()  
  8.     {  
  9.         _con = new SqlConnection();  
  10.         _con.ConnectionString = ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;  
  11.         _cmd = new SqlCommand();  
  12.         _cmd.Connection = _con;  
  13.           
  14.     }  
  15.     public DataTable GetTable(string Query)  
  16.     {  
  17.         _da = new SqlDataAdapter(Query, _con);  
  18.         _dt = new DataTable();  
  19.         _da.Fill(_dt);  
  20.         return _dt;  
  21.     }  
  22.     public int RunDML(string Query)  
  23.     {  
  24.         int res;  
  25.         _cmd.CommandText = Query;  
  26.         _con.Open();  
  27.         res = _cmd.ExecuteNonQuery();  
  28.         _con.Close();  
  29.         return res;  
  30.     }  
  31. }  
Step 2: Open Visual Studio and create a new website and after that write the following code in your aspx page.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>  
  2.     <!DOCTYPE html>  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title>Repeater Control</title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div>  
  12.                 <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">  
  13.                     <HeaderTemplate>  
  14.                         <table style="width: 100%">  
  15.                             <tr style="background-color: blue">  
  16.                                 <th>EmpID</th>  
  17.                                 <th>Name</th>  
  18.                                 <th>Salary</th>  
  19.                                 <th>City</th>  
  20.                                 <th>  
  21.                                     <asp:CheckBox ID="ChkAll" Text="CheckAll" runat="server" OnCheckedChanged="ChkAll_CheckedChanged" AutoPostBack="true" />  
  22.                                 </th>  
  23.                                 <th>  
  24.                                     <asp:Button ID="btndel" runat="server" Text="DeleteAll" CommandName="delall" />  
  25.                                 </th>  
  26.                                 <th>  
  27.                                     <asp:Label ID="Editupdate" Text="UpdateAll" runat="server"></asp:Label>  
  28.                                 </th>  
  29.                             </tr>  
  30.                     </HeaderTemplate>  
  31.                     <ItemTemplate>  
  32.                         <tr style="background-color:aqua; font-style:italic; text-align: center">  
  33.                             <td>  
  34.                                 <asp:Label ID="lbleID" runat="server" Text='<%#Eval("EmpID") %>'></asp:Label>  
  35.                                 <asp:TextBox ID="txtid" runat="server" Text='<%#Eval("EmpID") %>' Visible="false"></asp:TextBox>  
  36.                                 <asp:HiddenField ID="hdid" runat="server" Value='<%#Eval("EmpID") %>' />  
  37.                                 <td>  
  38.                                     <asp:Label ID="lblname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>  
  39.                                     <asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>' Visible="false"></asp:TextBox>  
  40.                                 </td>  
  41.                                 <td>  
  42.                                     <asp:Label ID="lblsalary" runat="server" Text='<%#Eval("salary") %>'></asp:Label>  
  43.                                     <asp:TextBox ID="txtsalary" runat="server" Text='<%#Eval("salary") %>' Visible="false"></asp:TextBox>  
  44.                                 </td>  
  45.                                 <td>  
  46.                                     <asp:Label ID="lblcity" runat="server" Text='<%#Eval("City") %>'></asp:Label>  
  47.                                     <asp:TextBox ID="City" runat="server" Text='<%#Eval("City") %>' Visible="false"></asp:TextBox>  
  48.                                 </td>  
  49.                                 <td>  
  50.                                     <asp:CheckBox ID="chk" runat="server" />  
  51.                                 </td>  
  52.                                 <td>  
  53.                                     <asp:Button ID="btndel" runat="server" Text="Delete" CommandName="del" CommandArgument='<%#Eval("EmpID") %>' />  
  54.                                 </td>  
  55.                                 <td>  
  56.                                     <asp:LinkButton ID="Lbtn1" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>  
  57.                                     <asp:LinkButton ID="LinkButton2" runat="server" Text="Update"></asp:LinkButton>  
  58.                                 </td>  
  59.                         </tr>  
  60.                     </ItemTemplate>  
  61.                     <AlternatingItemTemplate>  
  62.                         <tr style="background-color: gray; font-style:italic ;text-align: center">  
  63.                             <td>  
  64.                                 <asp:Label ID="lbleID" runat="server" Text='<%#Eval("EmpID") %>'></asp:Label>  
  65.                                 <asp:TextBox ID="txtid" runat="server" Text='<%#Eval("EmpID") %>' Visible="false"></asp:TextBox>  
  66.                                 <asp:HiddenField ID="hdid" runat="server" Value='<%#Eval("EmpID") %>' />  
  67.                                 <td>  
  68.                                     <asp:Label ID="lblname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>  
  69.                                     <asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>' Visible="false"></asp:TextBox>  
  70.                                 </td>  
  71.                                 <td>  
  72.                                     <asp:Label ID="lblsalary" runat="server" Text='<%#Eval("salary") %>'></asp:Label>  
  73.                                     <asp:TextBox ID="txtsalary" runat="server" Text='<%#Eval("salary") %>' Visible="false"></asp:TextBox>  
  74.                                 </td>  
  75.                                 <td>  
  76.                                     <asp:Label ID="lblcity" runat="server" Text='<%#Eval("City") %>'></asp:Label>  
  77.                                     <asp:TextBox ID="City" runat="server" Text='<%#Eval("City") %>' Visible="false"></asp:TextBox>  
  78.                                 </td>  
  79.                                 <td>  
  80.                                     <asp:CheckBox ID="chk" runat="server" />  
  81.                                 </td>  
  82.                                 <td>  
  83.                                     <asp:Button ID="btndel" runat="server" Text="Delete" CommandName="del" CommandArgument='<%#Eval("EmpID") %>' />  
  84.                                 </td>  
  85.                                 <td>  
  86.                                     <asp:LinkButton ID="Lbtn1" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>  
  87.                                     <asp:LinkButton ID="LinkButton2" runat="server" Text="Update"></asp:LinkButton>  
  88.                                 </td>  
  89.                         </tr>  
  90.                     </AlternatingItemTemplate>  
  91.                 </asp:Repeater>  
  92.             </div>  
  93.         </form>  
  94.     </body>  
  95.   
  96.     </html>  
Step 3: Bind DataBase Table using SQL Helper Class.
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.     {  
  5.         Binding();  
  6.     }  
  7. }     
  8. private void Binding()  
  9. {    
  10.         SqlHelper sh = new SqlHelper();  
  11.         Repeater1.DataSource = sh.GetTable("select * from Emp");  
  12.         Repeater1.DataBind();   
  13. }  
Step 4: Hare is the code behind page for Edit, Update and Delete operations.
  1. protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)  
  2. {  
  3.     if (e.CommandName == "delall")  
  4.     {  
  5.         string query = "Delete from Emp where EmpID in (0";  
  6.         foreach (RepeaterItem a in Repeater1.Items)  
  7.         {  
  8.             if (a.ItemIndex >= 0)  
  9.             {  
  10.                 CheckBox chk = a.FindControl("chk"as CheckBox;  
  11.                 if (chk.Checked)  
  12.                 {  
  13.                     query += "," + (a.FindControl("hdid"as HiddenField).Value;  
  14.                 }  
  15.             }  
  16.   
  17.         }  
  18.         query += ")";  
  19.   
  20.         if ((new SqlHelper().RunDML(query) > 0))  
  21.         {  
  22.             Binding();  
  23.         }  
  24.     }  
  25.     else if (e.CommandName == "del")  
  26.     {  
  27.         string query = "Delete from Emp where EmpID = " + e.CommandArgument;  
  28.         if ((new SqlHelper().RunDML(query) > 0))  
  29.         {  
  30.             if (IsPostBack)  
  31.             {  
  32.                 Binding();  
  33.   
  34.             }  
  35.         }  
  36.   
  37.     }  
  38.     else if (e.CommandName == "Edit" )  
  39.     {  
  40.   
  41.         foreach (RepeaterItem a in Repeater1.Items)  
  42.         {  
  43.             if (a.ItemIndex == 1)  
  44.             {  
  45.                 Label lbl1 = a.FindControl("lbleID"as Label;  
  46.                 lbl1.Visible = false;  
  47.                 Label lbl2 = a.FindControl("lblname"as Label;  
  48.                 lbl2.Visible = false;  
  49.                 Label lbl3 = a.FindControl("lblsalary"as Label;  
  50.                 lbl3.Visible = false;  
  51.                 Label lbl4 = a.FindControl("lblcity"as Label;  
  52.                 lbl4.Visible = false;  
  53.   
  54.                 TextBox txt1 = a.FindControl("txtid"as TextBox;  
  55.                 txt1.Visible = true;  
  56.                 TextBox txt2 = a.FindControl("txtname"as TextBox;  
  57.                 txt2.Visible = true;  
  58.                 TextBox txt3 = a.FindControl("txtsalary"as TextBox;  
  59.                 txt3.Visible = true;  
  60.                 TextBox txt4 = a.FindControl("City"as TextBox;  
  61.                 txt4.Visible = true;  
  62.   
  63.   
  64.            }  
  65.         }  
  66.          
  67.     }  
  68. }  
Output Design will look like the following screenshot:

output 

Up Next
    Ebook Download
    View all
    Learn
    View all