<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register TagName="ChildRepeater" TagPrefix="SamplesUC" Src="~/categories/ASPNET1/HierarchicalData/NestedRepeater.ascx" %>
<%@ Import Namespace="System.Data" %>
<asp:content id="Content1" contentplaceholderid="head" runat="server">
</asp:content>
<script runat="server" language="C#">
private DataSet CreateDS()
{
DataSet ds;
if (Session["Repeater_ParentChild"] == null)
{
ds = new DataSet();
DataTable dt = new DataTable("Company");
DataRow dr;
dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyName", typeof(string)));
dt.Columns.Add(new DataColumn("Address", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Dept", typeof(string)));
for (int i = 1; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Company " + i;
dr[2] = "Address " + i;
dr[3] = "Manager name";
dr[4] = "Adminstration";
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
DataColumn[] Parent_PKColumns = new DataColumn[1];
Parent_PKColumns[0] = dt.Columns["ID"];
dt.PrimaryKey = Parent_PKColumns;
dt = new DataTable("Employees");
dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyID", typeof(Int32)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Dept", typeof(string)));
for (int i = 1; i < 10; i++)
{
int imax = 0;
if (i % 2 == 0) imax = 5;
else imax = 4;
for (int y = 2; y < imax; y++) //3 emplyees for each company
{
dr = dt.NewRow();
dr[0] = y + i * 5;
dr[1] = i;
dr[2] = "Employee # " + dr[0];
dr[3] = "Dept # " + (y + i);
dt.Rows.Add(dr);
}
}
DataColumn[] Child_PKColumns = new DataColumn[1];
Child_PKColumns[0] = dt.Columns["ID"];
dt.PrimaryKey = Child_PKColumns;
ds.Tables.Add(dt);
DataColumn[] Child_FKColumns = new DataColumn[1];
Child_FKColumns[0] = dt.Columns["CompanyID"];
DataRelation datarel = new DataRelation("ParentChild", Parent_PKColumns,
Child_FKColumns);
ds.Relations.Add(datarel);
Session["Repeater_ParentChild"] = ds;
}
else
{
ds = (DataSet)Session["Repeater_ParentChild"];
}
return ds;
}
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) Repeater_DataBind();
}
private void Repeater_DataBind()
{
ParentRepeater.DataSource = CreateDS().Tables["Company"];
ParentRepeater.DataBind();
}
protected void ParentRepeater_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.EditItem)
{
int iPrimaryKey = Convert.ToInt16(((HtmlInputHidden)e.Item.FindControl("PK")).Value);
WEBSWAPP.Website.CodeSamples.ASPNET1.HierarchicalData.NestedRepeater ChildList = (WEBSWAPP.Website.CodeSamples.ASPNET1.HierarchicalData.NestedRepeater)e.Item.FindControl("ChildList1");
if (ChildList == null) ChildList = (WEBSWAPP.Website.CodeSamples.ASPNET1.HierarchicalData.NestedRepeater)e.Item.FindControl("ChildList2");
if (ChildList != null) ChildList.CompanyPrimaryKey = iPrimaryKey;
}
}
protected void SelectGroup(object sender, System.EventArgs e)
{
RadioButton chk = (RadioButton)sender;
RepeaterItem dlItem = (RepeaterItem)chk.NamingContainer;
if (chk.Checked) { ((HtmlTableRow)dlItem.FindControl("ParentRow")).Attributes.Add("class", "SelectedGridItem"); }
foreach (RepeaterItem dlitem in ParentRepeater.Items)
{
RadioButton rb = (RadioButton)dlitem.FindControl("chkSelect");
if (rb != null)
{
if (rb != chk)
{
rb.Checked = false;
((HtmlTableRow)dlitem.FindControl("ParentRow")).Attributes["class"] = "";
}
}
}
}
</script>
<asp:content id="Content2" contentplaceholderid="MainContent" runat="server">
<table class="tblBody">
<tr>
<td>
<h4>Demo for nested Repeaters to represent hierarchical information</h4>
<asp:Repeater ID="ParentRepeater" Runat =server OnItemDataBound ="ParentRepeater_ItemDataBound" >
<HeaderTemplate >
<table border=0 cellpadding=0 cellspacing=0 class="DataListStyle2"">
<tr class="DataList1HeaderStyle">
<td>Company Name</td>
<td>Employee Name</td>
<td>Department</td>
</tr>
</HeaderTemplate>
<ItemTemplate >
<!--- this row would have the parent Grid--->
<tr runat=server id="ParentRow">
<td colspan=3>
<asp:RadioButton ID="chkSelect" title="click here to select this group"
AutoPostBack=True Runat=server OnCheckedChanged ="SelectGroup"></asp:RadioButton>
<asp:Label Runat=server Text='<%# DataBinder.Eval(Container.DataItem, "CompanyName")%>' ID="Label1"></asp:Label>
<input type="hidden" id="PK" runat=server value='<%# DataBinder.Eval(Container.DataItem, "ID")%>'>
</td>
</tr>
<!--- this row would have the child Repeater-->
<tr >
<td colspan =3>
<SamplesUC:ChildRepeater id="ChildList1" Runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate >
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</asp:content>