Repeater Control in ASP.NET
In this article I will explain the repeater control, uses of repeater control and how one can implement repeater control in asp.net.
Repeater Control is a control which is used to display the repeated list of items. Like if we need to display the prescription of a doctor which he/she gave to the client. Here we can use of it.
Repeater Control in ASP.NET
In this article I will explain the repeater control, uses of repeater control and how one can implement repeater control in asp.net.
Repeater Control is a control which is used to display the repeated list of items. Like if we need to display the prescription of a doctor which he/she gave to the client. Here we can use of it.
The Repeater control works by looping through the records in your data source and then repeating the rendering of it's templates called item template. Repeater control contains different types of template fields those are
itemTemplate
AlternatingitemTemplate
HeaderTemplate
FooterTemplate
SeperatorTemplate
ItemTemplate: ItemTemplate defines how the 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. Usually, SeparateTemplate will be <br> html element or <hr> html element.
Create a table named details having rows-
Id
name
address
company
Now create a page named (suppose) repeater.aspx.
<div>
<asp:Repeater ID="RepDetails" runat="server">
<HeaderTemplate>
<table>
<tr>
<td colspan="2">
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<table>
<tr>
<td>
Subject:
<asp:Label ID="lblname" runat="server" Text='<%#Eval("name") %>'/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbladdress" runat="server" Text='<%#Eval("address") %>'/>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblcompany" runat="server" Text='<%#Eval("company") %>'/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
Now bind this reapeter control with the database table that you have created just above. In repeater.aspx.cs page have the following-
in the namespace add the below-
using System.Data;
using System.Data.SqlClient;
using system.configuration;
at page load method write the below-
if(!IsPostBack)
{
binddata();
}
now bind the data. If you want the data to be displayed at the page load it self write a method in page load event-
protected void binddata();
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from details Order By id desc", conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
RepDetails.DataSource = ds;
RepDetails.DataBind();
conn.Close();
}
and now just execute it and have a result.
Nice Explanation, While go with Repeater control we must take care of designing part. Designing part play key role while working with Repeater. Normally, for Gridview and other controls we simply drag and drop and assign data to that, if we want user defined columns then we set AutoGenerateColumns property to "false" but using repeater that's not possible. we must defined columns as we want using table contents. If we want to design Header columns then use HeaderTemplate, If we want to design Items list then we must use ItemTemplate..
For each and every template we need to design it separately. I will give you some sample design part how to design the Repeater control.