What is Repeater control?
The Repeater control is a Data-bound control that allows custom layout by repeating a specified template. The Repeater control is the only Web control that allows you to split markup tags across the templates.
For example:-
Begin table tag (<table>) in the HeaderTemplate, a single table row tag (<tr>) in the ItemTemplate, and the end of table tag (</table>) in the FooterTemplate.
Syntax:
<asp:Repeater ID="Repeater1" runat="server">
</asp:Repeater>
</asp:Repeater>
* Repeater control doesn't support any built-in layout to display data item. so it can be customized to create custom layout, using template.
These are the following templates supported by Repeater control:-
1. HeaderTemplate
2. ItemTemplate
3. AlternatingItemTemplate
4. SeparatorTemplate
5. FooterTemplate
*Repeater control has no built-in support for Editing, Sorting, Paging of data. But it can be done with customization of programming (using ItemCommand event).
* Important properties of Repeater control:-
1. DataSource / DataSourceID
2. DataMember
* Important method:-
1. DataBind()
* These above properties and method can be used to bind data to Repeater control from a database.
* Important Events of Repeater control:-
1. ItemCommand
2. ItemCreated
3. ItemDataBound
* Now,We should start example:-
First of all create database, table and insert some data to it. So, create a database named-TestDB then create tables Dept and Emp. In this example we will group by Department Id.Table structure is shown below-
![]() |
Emp Table |
![]() |
Dept Table |
Write these code to Web.config file for connection string
<configuration>
<connectionStrings>
<add name="conStr" connectionString="Data Source=.; Database=TestDB; User Id=yourSqlId; Password=yourSqlPassword;"/>
</connectionStrings>
</configuration>
<connectionStrings>
<add name="conStr" connectionString="Data Source=.; Database=TestDB; User Id=yourSqlId; Password=yourSqlPassword;"/>
</connectionStrings>
</configuration>
Now, Write the following code in RepeaterViewRecords.aspx. In the below code we took two Repeater control. First one called as- ParentRepeater and second childRepeater.
ParentRepeater contains DeptName and childRepeater contains rows for Emp table data.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterViewRecords.aspx.cs" Inherits="RepeaterViewRecords" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" type="text/css" href="css/RepeaterViewRecords.css" />
<script type="text/javascript" src="script/jquery-2.0.2.js"></script>
<script type="text/javascript" src="script/jquery-ui.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="RepeaterWrapper">
<div id="RepeaterScroll">
<asp:Repeater ID="ParentRepeater" OnItemDataBound="ParentRepeater_ItemDataBound" runat="server">
<HeaderTemplate>
<table align="center" style="max-height: 500px;">
<tr style="background-color: #07B3D3;">
<th class="row" colspan="4" style="padding: 5px;">Employee Data</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr align="left">
<th class="header" colspan="4" style="">Dept Name: <b><%# DataBinder.Eval(Container.DataItem, "DeptName") %></b></th>
</tr>
<tr>
<th class="row" style="padding:2px;">EmpId</th>
<th class="row" style="padding:2px;">EmpName</th>
<th class="row" style="padding:2px;">EmpJob</th>
<th class="row" style="padding:2px;">EmpSalary</th>
</tr>
<asp:Repeater ID="childRepeater" runat="server" OnItemDataBound="childRepeater_ItemDataBound" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>'>
<ItemTemplate>
<tr align="center">
<td style="padding:5px;">
<asp:Label ID="lblEmpId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "[\"EmpId\"]")%>' />
</td>
<td style="padding:5px;"><%# DataBinder.Eval(Container.DataItem, "[\"EmpName\"]")%></td>
<td style="padding:5px;"><%# DataBinder.Eval(Container.DataItem, "[\"EmpJob\"]")%></td>
<td style="padding:5px;">
<asp:Label ID="lblEmpSalary" runat="server" Text='<%#string.Format("{0:C}", DataBinder.Eval(Container.DataItem, "[EmpSalary]"))%>'></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr style="background-color: lightblue; text-align: center; font-weight: bold;">
<td colspan="2">No Of Employee: <asp:Label runat="server" Text='<%# this.empCount %>'></asp:Label></td>
<td colspan="2">Total Salary: <asp:Label runat="server" Text='<%# string.Format("{0:C}", this.salaryCount) %>'></asp:Label></td>
</tr>
<tr>
<td colspan="4" style="height: 20px;"></td>
</tr>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
<tr style="background-color: #07B3D3; text-align: center; font-weight: bold;">
<td class="row" colspan="2" style="padding: 5px;">Total No. Of Emplyoee: <%=this.totalEmpCount %></td>
<td class="row" colspan="2">Grand Total Of Salary: <%=string.Format("{0:C}",this.grandTotalSalary) %></td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
</div>
</form>
<script type="text/javascript">
$('#RepeaterWrapper').draggable({ scroll: false, cursor: "move" });
</script>
</body>
</html>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" type="text/css" href="css/RepeaterViewRecords.css" />
<script type="text/javascript" src="script/jquery-2.0.2.js"></script>
<script type="text/javascript" src="script/jquery-ui.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="RepeaterWrapper">
<div id="RepeaterScroll">
<asp:Repeater ID="ParentRepeater" OnItemDataBound="ParentRepeater_ItemDataBound" runat="server">
<HeaderTemplate>
<table align="center" style="max-height: 500px;">
<tr style="background-color: #07B3D3;">
<th class="row" colspan="4" style="padding: 5px;">Employee Data</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr align="left">
<th class="header" colspan="4" style="">Dept Name: <b><%# DataBinder.Eval(Container.DataItem, "DeptName") %></b></th>
</tr>
<tr>
<th class="row" style="padding:2px;">EmpId</th>
<th class="row" style="padding:2px;">EmpName</th>
<th class="row" style="padding:2px;">EmpJob</th>
<th class="row" style="padding:2px;">EmpSalary</th>
</tr>
<asp:Repeater ID="childRepeater" runat="server" OnItemDataBound="childRepeater_ItemDataBound" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>'>
<ItemTemplate>
<tr align="center">
<td style="padding:5px;">
<asp:Label ID="lblEmpId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "[\"EmpId\"]")%>' />
</td>
<td style="padding:5px;"><%# DataBinder.Eval(Container.DataItem, "[\"EmpName\"]")%></td>
<td style="padding:5px;"><%# DataBinder.Eval(Container.DataItem, "[\"EmpJob\"]")%></td>
<td style="padding:5px;">
<asp:Label ID="lblEmpSalary" runat="server" Text='<%#string.Format("{0:C}", DataBinder.Eval(Container.DataItem, "[EmpSalary]"))%>'></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr style="background-color: lightblue; text-align: center; font-weight: bold;">
<td colspan="2">No Of Employee: <asp:Label runat="server" Text='<%# this.empCount %>'></asp:Label></td>
<td colspan="2">Total Salary: <asp:Label runat="server" Text='<%# string.Format("{0:C}", this.salaryCount) %>'></asp:Label></td>
</tr>
<tr>
<td colspan="4" style="height: 20px;"></td>
</tr>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
<tr style="background-color: #07B3D3; text-align: center; font-weight: bold;">
<td class="row" colspan="2" style="padding: 5px;">Total No. Of Emplyoee: <%=this.totalEmpCount %></td>
<td class="row" colspan="2">Grand Total Of Salary: <%=string.Format("{0:C}",this.grandTotalSalary) %></td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
</div>
</form>
<script type="text/javascript">
$('#RepeaterWrapper').draggable({ scroll: false, cursor: "move" });
</script>
</body>
</html>
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
public partial class RepeaterViewRecords : System.Web.UI.Page
{
SqlConnection con = null;
SqlDataAdapter da = null;
DataSet ds = null;
string strCommand = "";
public int empCount = 0;
public int totalEmpCount = 0;
public double salaryCount = 0;
public double grandTotalSalary = 0;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
if(!Page.IsPostBack)
{
LoadParentData();
}
}
void LoadParentData()
{
strCommand = "select * from Dept";
da = new SqlDataAdapter(strCommand, con);
ds = new DataSet();
da.Fill(ds, "Dept");
ParentRepeater.DataSource = ds.Tables["Dept"];
SqlDataAdapter da1 = new SqlDataAdapter("select * from Emp", con);
da1.Fill(ds, "Emp");
ds.Relations.Add("myrelation",
ds.Tables["Dept"].Columns["DeptId"],
ds.Tables["Emp"].Columns["DeptId"]);
for (int i = 0; i < ds.Tables["Emp"].Rows.Count; i++)
{
grandTotalSalary += double.Parse(ds.Tables["Emp"].Rows[i]["EmpSalary"].ToString());
}
totalEmpCount = ds.Tables["Emp"].Rows.Count;
Page.DataBind();
}
protected void childRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
empCount++;
Label lblSalary = (Label)e.Item.FindControl("lblEmpSalary");
string tempSalary=lblSalary.Text.Remove(0, 1);
salaryCount += double.Parse(tempSalary);
if (double.Parse(tempSalary) < 20000)
{
lblSalary.ForeColor = Color.Red;
}
else if (double.Parse(tempSalary) >= 20000 && double.Parse(tempSalary) <= 40000)
lblSalary.ForeColor = Color.Blue;
else
lblSalary.ForeColor = Color.LimeGreen;
}
}
protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
salaryCount = 0;
empCount = 0;
}
}
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
public partial class RepeaterViewRecords : System.Web.UI.Page
{
SqlConnection con = null;
SqlDataAdapter da = null;
DataSet ds = null;
string strCommand = "";
public int empCount = 0;
public int totalEmpCount = 0;
public double salaryCount = 0;
public double grandTotalSalary = 0;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
if(!Page.IsPostBack)
{
LoadParentData();
}
}
void LoadParentData()
{
strCommand = "select * from Dept";
da = new SqlDataAdapter(strCommand, con);
ds = new DataSet();
da.Fill(ds, "Dept");
ParentRepeater.DataSource = ds.Tables["Dept"];
SqlDataAdapter da1 = new SqlDataAdapter("select * from Emp", con);
da1.Fill(ds, "Emp");
ds.Relations.Add("myrelation",
ds.Tables["Dept"].Columns["DeptId"],
ds.Tables["Emp"].Columns["DeptId"]);
for (int i = 0; i < ds.Tables["Emp"].Rows.Count; i++)
{
grandTotalSalary += double.Parse(ds.Tables["Emp"].Rows[i]["EmpSalary"].ToString());
}
totalEmpCount = ds.Tables["Emp"].Rows.Count;
Page.DataBind();
}
protected void childRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
empCount++;
Label lblSalary = (Label)e.Item.FindControl("lblEmpSalary");
string tempSalary=lblSalary.Text.Remove(0, 1);
salaryCount += double.Parse(tempSalary);
if (double.Parse(tempSalary) < 20000)
{
lblSalary.ForeColor = Color.Red;
}
else if (double.Parse(tempSalary) >= 20000 && double.Parse(tempSalary) <= 40000)
lblSalary.ForeColor = Color.Blue;
else
lblSalary.ForeColor = Color.LimeGreen;
}
}
protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
salaryCount = 0;
empCount = 0;
}
}
Now, write these code into RepeaterViewRecords.css
body {
background-color:rgba(128, 128, 128, 0.84);
/*background-image:url(/bg.jpg);*/
}
#RepeaterWrapper{
position:relative;
padding:30px 0 30px 0;
top:40px;
left:200px;
width:550px;
height:530px;
border:1px solid #808080;
display:block;
border-radius:5px;
box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 3px 7px rgba(0, 0, 0, 0.3), inset 0 1px rgba(255,255,255,1),inset 0 -3px 2px rgba(0,0,0,0.25);
background: linear-gradient(#eeefef, #ffffff 10%);
}
#RepeaterScroll{
position:relative;
padding:10px;
left:20px;
scrollbar-track-color:transparent;
max-height:500px;
max-width:500px;
overflow-y:scroll;
overflow-x:no-display;
}
.row{
-webkit-box-shadow: 0pt 2px 5px rgba(105, 108, 109, 0.7), 0px 0px 8px 5px rgba(208, 223, 226, 0.4) inset;
box-shadow: 0pt 2px 5px rgba(31, 135, 169, 0.70), 0px 0px 8px 5px rgba(208, 223, 226, 0.4) inset;
}
.header{
-webkit-box-shadow: 0pt 2px 5px rgba(198, 91, 21, 0.70), 0px 0px 8px 5px rgba(198, 91, 21, 0.70) inset;
box-shadow: 0pt 2px 3px rgba(198, 91, 21, 0.70), 0px 0px 8px 10px rgba(232, 201, 19, 0.70) inset;
}
#RepeaterScroll::-webkit-scrollbar-track
{
display:none;
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
background-color: #F5F5F5;
border-radius: 10px;
}
::-webkit-scrollbar-track
{
display:none;
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
background-color: #F5F5F5;
border-radius: 10px;
}
::-webkit-scrollbar
{
width: 10px;
background-color:#F5F5F5;
}
::-webkit-scrollbar-thumb
{
border-radius: 10px;
background-color: #FFF;
background-image: -webkit-gradient(linear, 40% 0%, 75% 84%, from(#d5d75f), to(#d5d75f), color-stop(.6,lightblue));
}
background-color:rgba(128, 128, 128, 0.84);
/*background-image:url(/bg.jpg);*/
}
#RepeaterWrapper{
position:relative;
padding:30px 0 30px 0;
top:40px;
left:200px;
width:550px;
height:530px;
border:1px solid #808080;
display:block;
border-radius:5px;
box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 3px 7px rgba(0, 0, 0, 0.3), inset 0 1px rgba(255,255,255,1),inset 0 -3px 2px rgba(0,0,0,0.25);
background: linear-gradient(#eeefef, #ffffff 10%);
}
#RepeaterScroll{
position:relative;
padding:10px;
left:20px;
scrollbar-track-color:transparent;
max-height:500px;
max-width:500px;
overflow-y:scroll;
overflow-x:no-display;
}
.row{
-webkit-box-shadow: 0pt 2px 5px rgba(105, 108, 109, 0.7), 0px 0px 8px 5px rgba(208, 223, 226, 0.4) inset;
box-shadow: 0pt 2px 5px rgba(31, 135, 169, 0.70), 0px 0px 8px 5px rgba(208, 223, 226, 0.4) inset;
}
.header{
-webkit-box-shadow: 0pt 2px 5px rgba(198, 91, 21, 0.70), 0px 0px 8px 5px rgba(198, 91, 21, 0.70) inset;
box-shadow: 0pt 2px 3px rgba(198, 91, 21, 0.70), 0px 0px 8px 10px rgba(232, 201, 19, 0.70) inset;
}
#RepeaterScroll::-webkit-scrollbar-track
{
display:none;
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
background-color: #F5F5F5;
border-radius: 10px;
}
::-webkit-scrollbar-track
{
display:none;
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
background-color: #F5F5F5;
border-radius: 10px;
}
::-webkit-scrollbar
{
width: 10px;
background-color:#F5F5F5;
}
::-webkit-scrollbar-thumb
{
border-radius: 10px;
background-color: #FFF;
background-image: -webkit-gradient(linear, 40% 0%, 75% 84%, from(#d5d75f), to(#d5d75f), color-stop(.6,lightblue));
}
No comments:
Post a Comment