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.


Comments

Author: naveensanagasetti29 Jul 2014 Member Level: Gold   Points : 10

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.


<asp:Repeater ID="rptGeneral" runat="server">
<HeaderTemplate>
<table style="border: 1px solid black; width: 95%" cellpadding="0">
<tr style="background-color: #58A0C4; color: White; width: 100%">
<td colspan="2" style="width: 95%" align="left">
<b>Comments</b>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table style="background-color: #EBEFF0; border-top: 1px dotted black; border-bottom: 1px solid black;
width: 95%">
<tr>
<td align="left">
<table style="background-color: #EBEFF0; border-top: 1px dotted black; border-bottom: 1px solid black;
width: 100%">
<tr>
<td>
Created By:
<asp:Label ID="lblCreatedBy" runat="server" ForeColor="#0000FF" Text='<%#Eval("OwnerIdName") %>' />
  On:<asp:Label ID="lblDate" runat="server" ForeColor="#0000FF" Text='<%#Eval("CreatedOn") %>' />
</td>
</tr>
</table>
</td>
</tr>
<tr style="background-color: #EBEFF0">
<td align="left">
Remarks:
<asp:Label ID="lblRemarks" runat="server" ForeColor="#0000FF" Text='<%#Eval("NoteText") %>' />
</td>
</tr>
<tr>
<td colspan="2">
 
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: