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

63 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

vijay kumar said...

Very Usefull..

CHEMUTU MANOJ 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.
anvesh nani said...

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

Sandeep said...

very good and helpful

Ramesh Rajak 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

Atul Singh 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.

Naisargee Danech 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?

Gummadi Haritha 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

Jayraj Modi said...

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

Gurprit singh said...

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

Jitesh Kumar Jha said...

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

Girish Kulkarni 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 :)

ziyad s.v 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?

Atul Rawat said...
This comment has been removed by the author.
sqlservermasters 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

Rajeev Kumar 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.

Give your Valuable Comments

Other Related Posts

© 2010-2012 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.