.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Post New Resource Bookmark and Share   

Repeater Control in ASP.NET

Posted By:Ashutosh Jha       Posted Date: May 31, 2014    Points: 200    Category:    URL: http://www.dotnetspark.com  

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.

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend