Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Repeater Control example in asp.net

Jan 14, 2012
Introduction:

In this article I will explain what is repeater control, uses of repeater control and how to bind data to repeater control in asp.net.


Description:
  
In previous posts I explained many articles regarding Gridview, Ajax, JQuery and many more. Now I will explain about what is repeater control, uses of repeater control, bind data to repeater control in asp.net.
What is Repeater Control?
Repeater Control is a control which is used to display the repeated list of items
Uses of Repeater Control
Repeater Control is used to display repeated list of items that are bound to the control and it’s same as gridview and datagridview. Repeater control is lightweight and faster to display data when compared with gridview and datagrid. By using this control we can display data in custom format but it’s not possible in gridview or datagridview and it doesn’t support for paging and sorting.  
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
1) itemTemplate 2) AlternatingitemTemplate 3) HeaderTemplate 4) FooterTemplate
5) 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.

To implement repeater control sample first design table in your database as shown below

Column Name
Data Type
Allow Nulls
Id
int(set identity property=true)
No
UserName
varchar(50)
Yes
Subject
nvarchar(MAX)
Yes
Comment
nvarchar(MAX)
Yes
PostedDate
datetime
Yes

After completion of table creation Open Visual Studio and create new website after that write the following code in your aspx page  


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Repeator Control Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Enter Name: </td>
<td><asp:TextBox ID="txtName" runat="server"/></td>
</tr>
<tr>
<td>Enter Subject: </td>
<td><asp:TextBox ID="txtSubject" runat="server"/></td>
</tr>
<tr>
<td valign="top">Enter Comments:</td>
<td><asp:TextBox ID="txtComment" runat="server" Rows="5" Columns="20" TextMode="MultiLine"/></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /></td>
</tr>
</table>
</div>
<div>
<asp:Repeater ID="RepDetails" runat="server">
<HeaderTemplate>
<table style=" border:1px solid #df5015; width:500px" cellpadding="0">
<tr style="background-color:#df5015; color:White">
<td colspan="2">
<b>Comments</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#EBEFF0">
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015; width:500px" >
<tr>
<td>
Subject:
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Subject") %>' Font-Bold="true"/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment") %>'/>
</td>
</tr>
<tr>
<td>
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015;border-bottom:1px solid #df5015; width:500px" >
<tr>
<td>Post By: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("UserName") %>'/></td>
<td>Created Date:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("PostedDate") %>'/></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
After completion of aspx page design add following namespaces in code behind

C# Code

using System;
using System.Data;
using System.Data.SqlClient;
After add namespace write the following code


private SqlConnection con = new SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true");
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindRepeaterData();
}
}
// This button click event is used to insert comment details and bind data to repeater control
protected void btnSubmit_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into Repeater_Table (UserName,Subject,Comment,PostedDate) values(@userName,@subject,@comment,@postedDate)", con);
cmd.Parameters.AddWithValue("@userName", txtName.Text);
cmd.Parameters.AddWithValue("@subject", txtSubject.Text);
cmd.Parameters.AddWithValue("@comment", txtComment.Text);
cmd.Parameters.AddWithValue("@postedDate", DateTime.Now);
cmd.ExecuteNonQuery();
con.Close();
txtName.Text = string.Empty;
txtSubject.Text = string.Empty;
txtComment.Text = string.Empty;
BindRepeaterData();
}
//Bind Data to Repeater Control
protected void BindRepeaterData()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from Repeater_Table Order By PostedDate desc", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
RepDetails.DataSource = ds;
RepDetails.DataBind();
con.Close();
}
VB.NET Code


Imports System.Data
Imports System.Data.SqlClient

Partial Public Class Default2
Inherits System.Web.UI.Page
Private con As New SqlConnection("Data Source=SureshDasari;Initial Catalog=MySampleDB;Integrated Security=true")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindRepeaterData()
End If
End Sub
' This button click event is used to insert comment details and bind data to repeater control
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
con.Open()
Dim cmd As New SqlCommand("insert into Repeater_Table (UserName,Subject,Comment,PostedDate) values(@userName,@subject,@comment,@postedDate)", con)
cmd.Parameters.AddWithValue("@userName", txtName.Text)
cmd.Parameters.AddWithValue("@subject", txtSubject.Text)
cmd.Parameters.AddWithValue("@comment", txtComment.Text)
cmd.Parameters.AddWithValue("@postedDate", DateTime.Now)
cmd.ExecuteNonQuery()
con.Close()
txtName.Text = String.Empty
txtSubject.Text = String.Empty
txtComment.Text = String.Empty
BindRepeaterData()
End Sub
'Bind Data to Repeater Control
Protected Sub BindRepeaterData()
con.Open()
Dim cmd As New SqlCommand("select * from Repeater_Table Order By PostedDate desc", con)
Dim ds As New DataSet()
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
RepDetails.Visible = True
RepDetails.DataSource = ds
RepDetails.DataBind()
Else
RepDetails.Visible = False
End If
con.Close()
End Sub
End Class
Now run in your application and check the output

Demo

Download sample code attached


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

105 comments :

Ajay Patil said...

Awesome , friend good job .............very helpful

SAUROV HALDER 333 said...

It's amazing ....The best site I have ever found for self learning...like to keep in touch...Keep it on...saurov.halder33@gmail.com

Nitin said...

great site thanx for all suresh

RaViNdRa PaRcHa said...

wooo.......great man .... Best learning site ever seen.

sahil gupta said...

awesome site site very helpful. keep posting.
sahilgupta562@gmail.com

priya said...

how to zoom that item on mouse hover?

Anonymous said...

really superb.

Nandi said...

your codings are really helpful. thanks Suresh

Anonymous said...

very useful thanks

Anonymous said...

sir,m big fan ur website ....every time it help in my projects....thank u sir...

sabanam said...

sir can u give me comment box code which u had used in ur website....

Rameshjogi said...

. Create a Blog Module where user can login and they can post their own blog and other users can review it and they can comment it but they won’t be having an option to delete and edit. Only owner will be having an option to delete and edit.

Suresh Dasari said...

@Rameshjogi....
if you want to implement like that means you should check the person who logged in user is the person who posted or not otherwise don't give permission for that....

Anonymous said...

hello-
I wanna to send mail through asp.net at remote server http//:xyz.in but we could't do this with my and yr codes also please tell me for the remote server to any other like-- abs@xyz....
with only
to-, from, subject-, body, attachment.

Imtiaz said...

how to validate it on button click event, if a title in the database is already exist then user should receive a message on label that change the title or it is already exist.

thank you

zeenat said...

i want to learn about multiview

hemanth said...

nice 1

Unknown said...

Very Usefull..

Unknown said...

its very useful ....thanks

Magaiana said...

Greeting fellow Devs. (C# & SQL) I need help, i want to displaying items of a specific order number on a repeater control as soon as i add them.. the order number source is a text-box control..

Magaiana said...

Greeting fellow Devs. (C# & SQL) I need help, i want to displaying items of a specific order number on a repeater control as soon as i add them.. the order number source is a text-box control..

Rathinamoorthy N said...

Awesome brother. All your posts are helping me a lot. Thanks :)

Anonymous said...

Really Superb...thanks

Anonymous said...

i need code with rply comment code

Arif Khan said...
This comment has been removed by the author.
Test said...

Id is not working when we did not allow the nulls,,,,

Sandeep said...

very good and helpful

Unknown said...

its very useful ....thanks

Atul said...

Hello Suresh,

It was vry useful..just keep it up.....

Anonymous said...

hi suresh

Anonymous said...

hi can you please tell or upload anything based on checkbox inside a gridiview which is in masterpage and another page which is content page which has datalist in it.. if we click on the checkbox inside gridview the datas should list in contentpage that is inside datalist using ajax or something... can you please upload it

Unknown said...

thanx buddy this is very useful to me

Ram said...

nice one

Roopesh Jain said...

there is an error at BindRepeatorData();
how to solve this

Anonymous said...

Nice example with detail explaination

venkatesh said...

can i implement this example for news update where admin of system update the news and user can view the news with image and more button.after clicking the more button user can view the hole news on new page.
like in your website the Recent Posts are working when user click on more button it displaying post on new page. please provide me code sir.

Unknown said...

U always help me in my work.. Thans sir.. I always use your site.. and also very helfull to me.. thanks..

Anonymous said...

nice...great job mr.suresh

Anonymous said...

Nice

S. Welsh said...

Thank you this is a great help for learning to code.
Sue

Anonymous said...

i am facing an error
'System.Data.DataRowView' does not contain a property with the name 'Subject'.
in using the Repeater Control.

Anonymous said...

can you please tell ,how to add dynamic column in repeater?

Unknown said...

thanks it is very helpfull for beginners....

Anonymous said...

hi, the comment label does not do a line break ..why??

Anonymous said...

sir which databasse u implement and i am gettiing error in Bindreprter()

pramod said...

Bindrepeaterdata()
get me error
which DB ur are using plzz tell me

Anonymous said...

sdgdfgsdfgsdfg

Anonymous said...

Thank u very much.It is too good.

Anonymous said...

Hello Sir,

This is om.I want to send email through asp.net at google mail server.So how to send,plz help me....thanks

Unknown said...

sir,
how can i add in repeater download and update panel link...
please reply..it most important in my prroject

hiiii said...

wow.. i was looking for this kind of tutorial...
thnx alot

Unknown said...

Awesome brother. All your posts are helping me a lot. Thanks :)

Unknown said...

hello sir. ur site is very useful, Thanx for the help.

i have a query regarding Repeater

'm having one button in Repeater and i want to fetch the value of 3Labels inside the same repeater. however i have managed to fetch value for 1 label bu i 'm not able to fetch other two..
please suggest something. thankyou :)

Unknown said...

impressive....thnx alot for your valuable codes

Anonymous said...

Hiiiiiiiiii,
I got a lots of knowledge from your sites.
Thanks a lot...........

Anonymous said...

nice one..thanks brother..

Shahabuddin said...

Sir this is really helpful,
sir tell me how to reply on a comment and is shows "1comment" under the Posted comment.

Anonymous said...

very helpful.....awesome explaination....

Anonymous said...

How to show images to the use of Repeater Control in .Net?

Svasth Gyan said...
This comment has been removed by the author.
Anonymous said...

I can see that you are are genuinely passionate about this! I am trying to build my own website and youve helped me with some great information.
http://www.sqlservermasters.com/

Atne said...

very good i enjoy it
thanks

Unknown said...

this code is not working after refresh the page at refresh i previous data is show on the page i want to no value at page load.

Lakshmi Narasimha said...

* Hi Brother,

Your site helps very well to learners & developers. But Now a days Stored Procedures are using in projects. So please write the articles with SPs now onwards. It is not at big task like you a eminent people.

Thank you.

Anonymous said...

nice

Anonymous said...

Is it possible to perform data deletion using asp.net repeater control?

Anonymous said...

How to display a default image if original image is not available in C# asp.net

Anonymous said...

I have for repeater control on one page. and have bind data using different ids all four from different ids how do i apply searching on them??

Unknown said...

thanks

Anonymous said...

Always valuable thank you sir.

Anonymous said...

Excellent

Unknown said...

how to print current id which is generated at time of submit button click on label text
please reply soon i really need it

rashed bin hares said...

Nice

Bvenkat Jeewesh said...

impressive....thnx alot for your valuable codes

Dhiraj said...

what is vb.net

Unknown said...

but without using datasource Plz implement the dataRepeater items

Anonymous said...

gud job sir

Unknown said...

Thanks Man For Your Great Action.. (Y)

Unknown said...

helpful..

Unknown said...

thank u very much

Anonymous said...

How to bind dynamically created datatable to repeater control..

I have done binding but it shows nothing

Unknown said...
This comment has been removed by the author.
Anonymous said...

hi,
This Article is very nice,
But I have a doubt,"If I insert an Like Imagebutton, i want to know the count of image button clicked". How can i implement that..!!
Plz help me..

Anonymous said...

what if I want to reply to a particular comment , instead of simply writing at the end...

Anonymous said...

How to bind created datatable to repeater control using radiobutton..

I have done binding but it shows nothing

Unknown said...

Superb sir thanks

Unknown said...

very nice code help me alot and now i want to make some changes in it once the user add first post then at the bottom of that post i add comment bt when i do next post then the post is added at the bottom now i want to do is i want to break this repeater for example when i add new post i ll display in new repeater

Unknown said...

Hi,
Suresh thank you for your reply but here my task is i have a html editor and when i submit text is displaying in gridview row here the question is i gave a hyperlink in the same grid row when i submit text the text should be saved in the form of pdf file and when ever i click on hyperlink i can be able to download the text as pdf file and also i want a reply button or a link in grid row where i can be able to reply for the particular comment pls help me as soon as possible

Bhavesh Bhuva said...

Very useful Example..
Thank You....

mp3musicmazaa said...

Hi brother
This example is useful
but tell me how to use Data pager with Repeater above example code
Please help me

Anonymous said...

very good explanation and quite enough to get the concept through

Unknown said...

how to reply for particular comment

Anonymous said...

Thank you so much and you have done good job

Anonymous said...

nice pagle.....

Unknown said...

nice work. Please how can i create a reply to each comment

Unknown said...

I have 10 records in my database. How can i show last 5 (6 to 10) records from database in repeater.

Anonymous said...

abcvfdsf 6546546

Anonymous said...

Your code runs into runtime Exception " System.Web.HttpException: You can only have one runat="server" control on a page."

Unknown said...

Very awesome sir !!!
You such a genius...

Anonymous said...

i want repeater output as the row format

Unknown said...

how can make nested comments reply..

Anonymous said...

Thank you! Very helpful.

Bond007 said...

please upload reply button cs code

arul said...

Dear sir,
Kindly provide with REPLY button

PostIndia said...

Thank you! Very helpful.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.