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.
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
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?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.
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"> </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
Now run in your application and check the output
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 |
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. |
|||
|
|||
105 comments :
Awesome , friend good job .............very helpful
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
great site thanx for all suresh
wooo.......great man .... Best learning site ever seen.
awesome site site very helpful. keep posting.
sahilgupta562@gmail.com
how to zoom that item on mouse hover?
really superb.
your codings are really helpful. thanks Suresh
very useful thanks
sir,m big fan ur website ....every time it help in my projects....thank u sir...
sir can u give me comment box code which u had used in ur website....
. 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.
@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....
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.
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
i want to learn about multiview
nice 1
Very Usefull..
its very useful ....thanks
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..
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..
Awesome brother. All your posts are helping me a lot. Thanks :)
Really Superb...thanks
i need code with rply comment code
Id is not working when we did not allow the nulls,,,,
very good and helpful
its very useful ....thanks
Hello Suresh,
It was vry useful..just keep it up.....
hi suresh
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
thanx buddy this is very useful to me
nice one
there is an error at BindRepeatorData();
how to solve this
Nice example with detail explaination
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.
U always help me in my work.. Thans sir.. I always use your site.. and also very helfull to me.. thanks..
nice...great job mr.suresh
Nice
Thank you this is a great help for learning to code.
Sue
i am facing an error
'System.Data.DataRowView' does not contain a property with the name 'Subject'.
in using the Repeater Control.
can you please tell ,how to add dynamic column in repeater?
thanks it is very helpfull for beginners....
hi, the comment label does not do a line break ..why??
sir which databasse u implement and i am gettiing error in Bindreprter()
Bindrepeaterdata()
get me error
which DB ur are using plzz tell me
sdgdfgsdfgsdfg
Thank u very much.It is too good.
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
sir,
how can i add in repeater download and update panel link...
please reply..it most important in my prroject
wow.. i was looking for this kind of tutorial...
thnx alot
Awesome brother. All your posts are helping me a lot. Thanks :)
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 :)
impressive....thnx alot for your valuable codes
Hiiiiiiiiii,
I got a lots of knowledge from your sites.
Thanks a lot...........
nice one..thanks brother..
Sir this is really helpful,
sir tell me how to reply on a comment and is shows "1comment" under the Posted comment.
very helpful.....awesome explaination....
How to show images to the use of Repeater Control in .Net?
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/
very good i enjoy it
thanks
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.
* 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.
nice
Is it possible to perform data deletion using asp.net repeater control?
How to display a default image if original image is not available in C# asp.net
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??
thanks
Always valuable thank you sir.
Excellent
how to print current id which is generated at time of submit button click on label text
please reply soon i really need it
Nice
impressive....thnx alot for your valuable codes
what is vb.net
but without using datasource Plz implement the dataRepeater items
gud job sir
Thanks Man For Your Great Action.. (Y)
helpful..
thank u very much
How to bind dynamically created datatable to repeater control..
I have done binding but it shows nothing
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..
what if I want to reply to a particular comment , instead of simply writing at the end...
How to bind created datatable to repeater control using radiobutton..
I have done binding but it shows nothing
Superb sir thanks
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
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
Very useful Example..
Thank You....
Hi brother
This example is useful
but tell me how to use Data pager with Repeater above example code
Please help me
very good explanation and quite enough to get the concept through
how to reply for particular comment
Thank you so much and you have done good job
nice pagle.....
nice work. Please how can i create a reply to each comment
I have 10 records in my database. How can i show last 5 (6 to 10) records from database in repeater.
abcvfdsf 6546546
Your code runs into runtime Exception " System.Web.HttpException: You can only have one runat="server" control on a page."
Very awesome sir !!!
You such a genius...
i want repeater output as the row format
how can make nested comments reply..
Thank you! Very helpful.
please upload reply button cs code
Dear sir,
Kindly provide with REPLY button
Thank you! Very helpful.
Note: Only a member of this blog may post a comment.