How to create ASP.NET Repeater Control Dynamically Runtime
Creating an ASP.NET Repeater Dynamically to an Object List
In previous article I explained about ASP.Net Repeater Control C# Example, Binding jQuery Accordion to DataTable in ASP .Net.
In this article I will explain about how to create a repeater control & binding dynamically, and creating item templates dynamically in runtime. Find the source code below:-
Default.aspx
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>C# - Bind data to a repeater
in runtime</title>
<style type="text/css">
.accordianContainer {
max-height:
350px;
overflow:
auto;
}
/*----------------Data
Grid inside Pop up----------------------*/
.CompApp_TableScroll {
max-height:
255px;
overflow:
auto;
}
.CompApp_DataTable {
border-collapse:
collapse;
}
.CompApp_DataTable tr th {
background-color: #dedede;
color:
#333333;
padding:
5px;
border:
1px solid #cccccc;
font-family:
Arial, Helvetica, sans-serif;
font-size:
12px;
font-weight:
normal;
white-space:
nowrap;
}
.CompApp_DataTable tr:nth-child(2n+2) {
background-color: #f3f4f5;
}
.CompApp_DataTable tr:nth-child(2n+1) td {
background-color: #d6dadf;
color:
#454545;
}
.CompApp_DataTable tr td {
padding:
5px;
color:
#454545;
font-family:
Arial, Helvetica, sans-serif;
font-size:
11px;
border:
1px solid #cccccc;
vertical-align:
middle;
text-align:
left;
white-space:
nowrap;
}
.CompApp_DataTable tr td:first-child {
text-align: left;
font-weight: bold;
text-align: left;
}
/*-------------------------*/
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="divAccord" class="accordianContainer" runat="server">
</div>
</form>
</body>
</html>
Default.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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeater();
}
}
private void BindRepeater()
{
try
{
Repeater rContainer = null;
DataTable dt = new
DataTable();
dt.Columns.Add("EmpId", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Address", typeof(string));
dt.Rows.Add(25, "Rk", "Gurgaon");
dt.Rows.Add(50, "Sachin", "Noida");
dt.Rows.Add(10, "Nitin", "Noida");
dt.Rows.Add(21, "Aditya", "Meerut");
dt.Rows.Add(100, "Mohan", "Banglore");
DataSet ds = new
DataSet();
ds.Tables.Add(dt);
if
(dt.Rows.Count > 0)
{
rContainer = new
Repeater();
rContainer.DataSource = dt;
rContainer.DataBind();
foreach (DataTable
dtCluster in ds.Tables)
{
rContainer = new Repeater();
rContainer.ItemTemplate = new RepeaterTemplate(ListItemType.Item);
rContainer.HeaderTemplate =
new RepeaterTemplate(ListItemType.Header);
rContainer.FooterTemplate =
new RepeaterTemplate(ListItemType.Footer);
rContainer.DataSource =
dtCluster;
divAccord.Controls.Add(rContainer);
this.DataBind();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
RepeaterTemplate.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
///
<summary>
///
Summary description for RepeaterTemplate
///
</summary>
public class RepeaterTemplate : System.Web.UI.ITemplate
{
ListItemType vTemplateItemType = ListItemType.Separator;
public RepeaterTemplate(ListItemType pListItemType)
{
vTemplateItemType = pListItemType;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
//lc.Text =
"Item number: " + itemcount.ToString() + "<BR>";
switch (vTemplateItemType)
{
// if condition to add HeaderTemplate
Dynamically only Once
case
ListItemType.Header:
lc.Text = "<h4> C# -
Binding Repeater Control Dynamically in ASP.Net </h4> <div
class='CompApp_TableScroll'><table class='CompApp_DataTable'
width='100%'>";
lc.Text += "<tr>";
lc.Text += "<td>";
lc.Text += "<u>EmpID</u>";
lc.Text += "</td>";
lc.Text += "<td>";
lc.Text += "<u>Name</u>";
lc.Text += "</td>";
lc.Text += "<td>";
lc.Text += "<u>Address</u>";
lc.Text += "</td>";
lc.Text += "</tr>";
break;
// if condition to add FooterTemplate
Dynamically only Once
case
ListItemType.Footer:
lc.Text = "</table></br>Footer
Text ..</div>";
break;
case
ListItemType.Item:
; break;
}
lc.DataBinding += new EventHandler(lc_DataBinding);
container.Controls.Add(lc);
}
private void lc_DataBinding(object sender, EventArgs e)
{
Literal vLiteral = (Literal)sender;
RepeaterItem vContainer = (RepeaterItem)vLiteral.NamingContainer;
DataRowView row = (DataRowView)vContainer.DataItem;
if (row != null)
{
vLiteral.Text += "<tr>";
for
(int i = 0; i <
row.DataView.Table.Columns.Count; i++)
{
vLiteral.Text += "<td>" + row[i].ToString() + "</td>";
}
vLiteral.Text += "</tr>";
}
}
}
Download the source code, click here ..
Please leave your comments, suggestions and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading! :)
How to create ASP.NET Repeater Control Dynamically Runtime
Reviewed by Ravi Kumar
on
12:01 PM
Rating:
thank you sir.. :)
ReplyDelete