Repeater control is used to show a repeated list of items from data source like data table, database, xml file, list etc. in Asp.Net that are bound to the control. Basically Asp.Net Repeater control is nothing but a container that shows the data from available data source in table format.Repeater control provides us a table layout. There are different types of template exists in Repeater.
Header Template:
It renders on the top of the control and show the header data.
Footer Template:
It renders on the bottom of the control and use to show the footer data like paging.
Item Template:
It is main template which is used to display the data from database, xml, list and data table.
Alternating Item Template:
It is used as like Item Template but It renders once after other data display. Basically use of alternating item template is providing look and style of alternative rows like background color, font etc.
Separator Template:
It renders after each item. For example a line after every record,
Advantage of Repeater Control
Repeater is the fastest control in data controls available in Asp.Net. So, we can say performance of repeater control is far better than other data control like GridView. If you only want to display data than the best choice is Repeater. But if want to use the entire feature predefined like paging, shorting, editable controls then you can choose GridView or other. Even if you can use repeater for this type of feature but you have to write code manually to create this feature with Repeater control.
Example
I am going to show an example how to create a repeater control in Asp.Net and bind the data to it.
RepeaterControlPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RepeaterControlPage.aspx.cs" Inherits="RepeaterControlExample.RepeaterControlPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style>
.header {
background-color:#808080;
color:#ffffff;
width:100%;
}
.item {
background-color:#ffffff;
width:100%;
}
.alternativeitem {
background-color:#0094ff;
color:#ffffff;
width:100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="rptStudent">
<HeaderTemplate>
<table>
<tr class="header">
<th>
<b>Student Id</b>
</th>
<th>
<b>Name</b>
</th>
<th>
<b>Address</b>
</th>
<th>
<b>Roll No</b>
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="item">
<td>
<%# Eval("StudentId") %>
</td>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Address") %>
</td>
<td>
<%# Eval("RollNo") %>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="alternativeitem">
<td>
<%# Eval("StudentId") %>
</td>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Address") %>
</td>
<td>
<%# Eval("RollNo") %>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
<tr>
<td colspan="4">
<b>Footer, use for paging</b>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
RepeaterControlPage.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace RepeaterControlExample
{
public partial class RepeaterControlPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetStudentList();
}
}
public void GetStudentList()
{
DataTable dtStudent = new DataTable();
dtStudent.Columns.Add("StudentId",typeof(int));
dtStudent.Columns.Add("Name", typeof(string));
dtStudent.Columns.Add("Address", typeof(string));
dtStudent.Columns.Add("RollNo", typeof(int));
dtStudent.Rows.Add(101, "Rahul Singh", "New Delhi", 1);
dtStudent.Rows.Add(102, "Mahesh Kumar", "Punjabi Bagh", 2);
dtStudent.Rows.Add(103, "Mandeep Singh", "Jashola", 3);
dtStudent.Rows.Add(104, "Banky", "Ashok Nagar", 4);
dtStudent.Rows.Add(105, "Mukesh Kumar", "Nangloi", 5);
try
{
if (dtStudent.Rows.Count > 0)
{
rptStudent.DataSource = dtStudent;
rptStudent.DataBind();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
Conclusion:
So, Today we learned what is Repeater control in Asp.Net and what is the advantage to use repeater rather than other data control available in Asp.Net.
I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks
Posted Comments :
sadhana Posted : 5 Years Ago
DFJD
xxxx Posted : 2 Years Ago
test
xxxx Posted : 2 Years Ago
test
xxxx Posted : 2 Years Ago
test
xxxx Posted : 2 Years Ago
test
rerer Posted : Last Year
ewer
rerer Posted : Last Year
ewer
rerer Posted : Last Year
ewer
rerer Posted : Last Year
ewer
fg Posted : Last Year
asd
naveen kumar Posted : 5 Years Ago
Good job