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.
- public class SqlHelper
- {
- SqlConnection _con;
- SqlCommand _cmd;
- SqlDataAdapter _da;
- DataTable _dt;
- public SqlHelper()
- {
- _con = new SqlConnection();
- _con.ConnectionString = ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
- _cmd = new SqlCommand();
- _cmd.Connection = _con;
- }
- public DataTable GetTable(string Query)
- {
- _da = new SqlDataAdapter(Query, _con);
- _dt = new DataTable();
- _da.Fill(_dt);
- return _dt;
- }
- public int RunDML(string Query)
- {
- int res;
- _cmd.CommandText = Query;
- _con.Open();
- res = _cmd.ExecuteNonQuery();
- _con.Close();
- return res;
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Repeater Control</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
- <HeaderTemplate>
- <table style="width: 100%">
- <tr style="background-color: blue">
- <th>EmpID</th>
- <th>Name</th>
- <th>Salary</th>
- <th>City</th>
- <th>
- <asp:CheckBox ID="ChkAll" Text="CheckAll" runat="server" OnCheckedChanged="ChkAll_CheckedChanged" AutoPostBack="true" />
- </th>
- <th>
- <asp:Button ID="btndel" runat="server" Text="DeleteAll" CommandName="delall" />
- </th>
- <th>
- <asp:Label ID="Editupdate" Text="UpdateAll" runat="server"></asp:Label>
- </th>
- </tr>
- </HeaderTemplate>
- <ItemTemplate>
- <tr style="background-color:aqua; font-style:italic; text-align: center">
- <td>
- <asp:Label ID="lbleID" runat="server" Text='<%#Eval("EmpID") %>'></asp:Label>
- <asp:TextBox ID="txtid" runat="server" Text='<%#Eval("EmpID") %>' Visible="false"></asp:TextBox>
- <asp:HiddenField ID="hdid" runat="server" Value='<%#Eval("EmpID") %>' />
- <td>
- <asp:Label ID="lblname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
- <asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>' Visible="false"></asp:TextBox>
- </td>
- <td>
- <asp:Label ID="lblsalary" runat="server" Text='<%#Eval("salary") %>'></asp:Label>
- <asp:TextBox ID="txtsalary" runat="server" Text='<%#Eval("salary") %>' Visible="false"></asp:TextBox>
- </td>
- <td>
- <asp:Label ID="lblcity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
- <asp:TextBox ID="City" runat="server" Text='<%#Eval("City") %>' Visible="false"></asp:TextBox>
- </td>
- <td>
- <asp:CheckBox ID="chk" runat="server" />
- </td>
- <td>
- <asp:Button ID="btndel" runat="server" Text="Delete" CommandName="del" CommandArgument='<%#Eval("EmpID") %>' />
- </td>
- <td>
- <asp:LinkButton ID="Lbtn1" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>
- <asp:LinkButton ID="LinkButton2" runat="server" Text="Update"></asp:LinkButton>
- </td>
- </tr>
- </ItemTemplate>
- <AlternatingItemTemplate>
- <tr style="background-color: gray; font-style:italic ;text-align: center">
- <td>
- <asp:Label ID="lbleID" runat="server" Text='<%#Eval("EmpID") %>'></asp:Label>
- <asp:TextBox ID="txtid" runat="server" Text='<%#Eval("EmpID") %>' Visible="false"></asp:TextBox>
- <asp:HiddenField ID="hdid" runat="server" Value='<%#Eval("EmpID") %>' />
- <td>
- <asp:Label ID="lblname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
- <asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>' Visible="false"></asp:TextBox>
- </td>
- <td>
- <asp:Label ID="lblsalary" runat="server" Text='<%#Eval("salary") %>'></asp:Label>
- <asp:TextBox ID="txtsalary" runat="server" Text='<%#Eval("salary") %>' Visible="false"></asp:TextBox>
- </td>
- <td>
- <asp:Label ID="lblcity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
- <asp:TextBox ID="City" runat="server" Text='<%#Eval("City") %>' Visible="false"></asp:TextBox>
- </td>
- <td>
- <asp:CheckBox ID="chk" runat="server" />
- </td>
- <td>
- <asp:Button ID="btndel" runat="server" Text="Delete" CommandName="del" CommandArgument='<%#Eval("EmpID") %>' />
- </td>
- <td>
- <asp:LinkButton ID="Lbtn1" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>
- <asp:LinkButton ID="LinkButton2" runat="server" Text="Update"></asp:LinkButton>
- </td>
- </tr>
- </AlternatingItemTemplate>
- </asp:Repeater>
- </div>
- </form>
- </body>
- </html>

Mysura ReddyPosted Sep 29, 2015, 4:38 AM
I think it is not fully completed code.. there is no Update Code.
Ajeet MishraPosted Sep 29, 2015, 1:58 AM
nice
Santhakumar MunuswamyPosted Sep 28, 2015, 2:07 PM
Good one
Muhammad Aqib ShehzadPosted Sep 28, 2015, 8:27 AM
good
Kundan JhaPosted Sep 28, 2015, 6:00 AM
thanks All....
jeev prasadPosted Sep 28, 2015, 5:34 AM
nice...........
Vipul MalhotraPosted Sep 28, 2015, 5:08 AM
nice article
Sujeet SumanPosted Sep 28, 2015, 3:28 AM
Nice Sharing...........
Vinodh NarayananPosted Sep 28, 2015, 3:13 AM
nice share
Sibeesh VenuPosted Sep 28, 2015, 3:13 AM
Thanks for the share
RakeshPosted Sep 28, 2015, 3:12 AM
Good share