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.
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.
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 |
<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> |
using System; using System.Data; using System.Data.SqlClient; |
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(); } |
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 |
|
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 Email
|
|||
|
|
|


Subscribe by RSS
Subscribe by Email
63 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.