Introduction
A Repeater is a Data-bound control. Data-bound controls are container controls. It creates a link between the Data Source and the presentation UI to display the data. The repeater control is used to display a repeated list of items.
A Repeater has five inline templates to format it.
- <HeaderTemplate>: Displays Header text for a Data Source collection and applies a different style for the Header text.
- <AlternatingItemTemplate>: Changes the background color or style of alternating items in a Data Source collection.
- <Itemtemplate>: It defines how each item is rendered from the Data Source collection.
- <SeperatorTemplate>: It will determine the separator element that separates each item in the item collection. It will be a <br> or <Hr> HTML element.
- <FooterTemplate>: Displays a footer element for the Data Source collection.
Now, in this article, I am describing the Repeater control in ASP.NET and how to create a comment page in ASP.NET.
First I created a database "EmpDetail". Then I created a table in the database.
Table Query
CREATE TABLE [dbo].[Comment] (
NULL,
[Subject] [nvarchar](max) NULL,
[CommentOn] [nvarchar](max) NULL,
[Post_Date] [datetime] NULL
) ON [PRIMARY];
Repeter_Control_Example.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Repeter_Control_Example : System.Web.UI.Page
{
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RepeterData();
}
}
protected void btnSubmit_click(object sender, EventArgs e)
{
try
{
con.Open();
cmd = new SqlCommand("INSERT INTO Comment (UserName, Subject, CommentOn, Post_Date) VALUES (@userName, @subject, @comment, @date)", con);
cmd.Parameters.Add("@userName", SqlDbType.NVarChar).Value = txtName.Text.ToString();
cmd.Parameters.Add("@subject", SqlDbType.NVarChar).Value = txtSubject.Text.ToString();
cmd.Parameters.Add("@comment", SqlDbType.NVarChar).Value = txtComment.Text.ToString();
cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = DateTime.Now.Date;
cmd.ExecuteNonQuery();
con.Close();
txtName.Text = string.Empty;
txtSubject.Text = string.Empty;
txtComment.Text = string.Empty;
RepeterData();
}
catch (Exception ex)
{
txtComment.Text = ex.Message;
}
}
public void RepeterData()
{
con.Open();
cmd = new SqlCommand("SELECT * FROM Comment ORDER BY Post_Date DESC", con);
DataSet ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
RepterDetails.DataSource = ds;
RepterDetails.DataBind();
}
}


bilal khattakPosted Jan 3, 2019, 3:39 AM
Hi sir.......
JOSEPH LEGRANDPosted Jul 21, 2018, 6:28 PM
Hello, this is very helpful!I did the exact example, but I used mysql. The problem i am experiencing is as follows: i've got 3 users: John, Mike and Peter. If John Says: "Hello" it registers on my db with no problem and includes the user id and time. But when i use the repeater to get back the comments this is what it displays: John: Hello Mike: Hello Peter: Hello which is wrong because John said Hello not peter or mike
Aditya BasakPosted Mar 19, 2018, 9:05 AM
Cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = DateTime.Now.Date; its should be @post_date
Subhash KokarePosted Dec 4, 2017, 7:40 AM
same article on aspdotnet Suresh by the way article is nice..
Tahir RehmanPosted Sep 24, 2017, 3:41 AM
Nice easy to understand!please can you define different List and Repater.are you work on entriy framwork
Tim RooksPosted Oct 2, 2016, 5:39 PM
I keep getting error on the public void RepeterData() at the con.Open() area I have downloaded the zip file but it is for VB code.....
Upendra Pratap ShahiPosted Jun 23, 2015, 4:45 AM
good..
Rajeev KumarPosted Jun 15, 2013, 11:33 PM
this code is same as asp-suresh,,, there is one error i want refresh data when we again pafe load