SIGN UP MEMBER LOGIN:    
ARTICLE

Master-Detail Display using Nested Repeater Web Forms Control

Posted by Dipal Choksi Articles | WebForms Controls August 05, 2002
In this example, we will create a web page that displays a formatted view of data with a master detail hierarchy using nested Repeater controls. We will use the Northwind sample for our example. There are many different ways to accomplish this task. We will use data binding and data relations in an effort to get a simple solution.
Reader Level:

Introduction

In this example, we will create a web page that displays a formatted view of data with a master detail hierarchy using nested Repeater controls. We will use the Northwind sample for our example. There are many different ways to accomplish this task. We will use data binding and data relations in an effort to get a simple solution.

Sample In Action



Details

Step 1: Create the user interface

Our user interface consists mainly of two Repeater controls, one nested within the other.
1. Add a repeater control on the web page for displaying the customer name. Display the data-bound value of Company Name in the Item Template of the repeater. Add a repeater control to display the Order details for each customer record.

<H2> Orders List by Customer </H2>
<
asp:Repeater runat="server" id="RepCust" onItemDataBound="ShowOrders">
<
ItemTemplate>
<
h3><%# ((DataRowView)Container.DataItem)["CompanyName"] %> </h3>
<
asp:Repeater id="RepOrders" runat="server"></asp:Repeater>
</
ItemTemplate>
</
asp:Repeater>

2. Create a table structure within the nested Repeater "RepOrders" to display the details of the Orders as shown in the code snippet below. We display the OrderDate, RequiredDate, ShippedDate and Freight values for Orders for each customer in the ItemTemplate of the second repeater control. The code should now look as follows.

<H2> Orders List by Customer </H2>
<
asp:Repeater runat="server" id="RepCust" onItemDataBound="ShowOrders">
<
ItemTemplate>
<
h3><%# ((DataRowView)Container.DataItem)["CompanyName"] %> </h3>
<
asp:Repeater id="RepOrders" runat="server">
<
HeaderTemplate>
<
table border=1 >
<
tr>
<
td>Order Date</td>
<
td>Required Date</td>
<
td>Shipped Date</td>
<
td>Freight</td>
</
tr>
</
HeaderTemplate>
<
ItemTemplate>
<
tr>
<
td><%# ((DataRowView)Container.DataItem)["OrderDate"]%></td>
<
td><%# ((DataRowView)Container.DataItem)["RequiredDate"]%></td>
<
td><%# ((DataRowView)Container.DataItem)["ShippedDate"]%></td>
<
td><%# DataBinder.Eval(Container.DataItem, "Freight", "{0:c}")%> </td>
</
tr>
</
ItemTemplate>
<
FooterTemplate>
</
table>
</FooterTemplate>
</
asp:Repeater>
</
ItemTemplate>
</
asp:Repeater>

Step 2: Bind the data

1. We will now create and fill datasets to display the customers and orders data.

void Page_Load(Object sender, EventArgs e)
{
string strSql, strConn;
DataSet ds=
new DataSet();
strConn = "server=localhost;Initial Catalog=Northwind;uid=sa;pwd=";
strSql ="select * from Customers";
SqlDataAdapter daCust =
new SqlDataAdapter(strSql, strConn);
daCust.Fill(ds, "Customers");
strSql ="select * from Orders";
SqlDataAdapter daOrders =
new SqlDataAdapter(strSql, strConn);
daOrders.Fill(ds, "Orders");
}

2. Now we will add a DataRelation object which links each Customer to the corresponding Orders in the dataset.

void Page_Load(Object sender, EventArgs e)
{
string strSql, strConn;
DataSet ds=
new DataSet();
strConn = "server=localhost;Initial Catalog=Northwind;uid=sa;pwd=";
strSql ="select * from Customers";
SqlDataAdapter daCust =
new SqlDataAdapter(strSql, strConn);
daCust.Fill(ds, "Customers");
strSql ="select * from Orders";
SqlDataAdapter daOrders =
new SqlDataAdapter(strSql, strConn);
daOrders.Fill(ds, "Orders");
DataRelation rel =
new DataRelation("CustOrdRel",
ds.Tables["Customers"].Columns["CustomerID"],ds.Tables["Orders"].Columns["CustomerID"]);
ds.Relations.Add(rel);
}

3. As shown below, bind the customers data to the first repeater control "RepCust"

void Page_Load(Object sender, EventArgs e)
{
string strSql, strConn;
DataSet ds=
new DataSet();
strConn = "server=localhost;Initial Catalog=Northwind;uid=sa;pwd=";
strSql ="select * from Customers";
SqlDataAdapter daCust =
new SqlDataAdapter(strSql, strConn);
daCust.Fill(ds, "Customers");
strSql ="select * from Orders";
SqlDataAdapter daOrders =
new SqlDataAdapter(strSql, strConn);
daOrders.Fill(ds, "Orders");
DataRelation rel =
new DataRelation("CustOrdRel",
ds.Tables["Customers"].Columns["CustomerID"],ds.Tables["Orders"].Columns["CustomerID"]);
ds.Relations.Add(rel);
RepCust.DataSource =ds.Tables["Customers"].DefaultView;
RepCust.DataBind();
}

4. Now we will add the code which display the Order information for each Customer. This is the most important part of the article.
This function is added as the event handler for the ItemDataBound event for the first repeater control RepCust. As each row item is bound, we access the ItemData contained within the RepCust Repeater row item. We bind the nested repeater control RepOrders with the DataView of the Child Data table based on the DataRelation that we created in our Page_Load event using this ItemData control - we make use of the CreateChildView method of the RepeaterItem obtained in the ItemDataBound event. Since the RepOrders repeater control is nested within the RepCust repeater control, we need to use the FindControl method to access the RepOrders control.

void ShowOrders(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((Repeater)e.Item.FindControl("RepOrders")).DataSource=((DataRowView)e.Item.DataItem).CreateChildView("CustOrdRel");
((Repeater)e.Item.FindControl("RepOrders")).DataBind();
}
}

Complete Code Listing: nestedrep.aspx

<%@ Page Language="C#" Debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<
head>
<
title>Master Detail information in Data Repeater </title>
<
script runat="server">
void Page_Load(Object sender, EventArgs e)
{
string strSql, strConn;
DataSet ds=
new DataSet();
strConn = "server=localhost;Initial Catalog=Northwind;uid=sa;pwd=";
strSql ="select * from Customers";
SqlDataAdapter daCust =
new SqlDataAdapter(strSql, strConn);
daCust.Fill(ds, "Customers");
strSql ="select * from Orders";
SqlDataAdapter daOrders =
new SqlDataAdapter(strSql, strConn);
daOrders.Fill(ds, "Orders");
DataRelation rel =
new DataRelation("CustOrdRel", ds.Tables["Customers"].Columns["CustomerID"],ds.Tables
"Orders"].Columns["CustomerID"]);
ds.Relations.Add(rel);
RepCust.DataSource =ds.Tables["Customers"].DefaultView;
RepCust.DataBind();
}
void ShowOrders(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
istItemType.AlternatingItem)
{
((Repeater)e.Item.FindControl("RepOrders")).DataSource= ((DataRowView)e.Item.DataItem).CreateChildView
"CustOrdRel");
((Repeater)e.Item.FindControl("RepOrders")).DataBind();
}
}
</script>
</
head>
<
body>
<
H2> Orders List by Customer </H2>
<
asp:Repeater runat="server" id="RepCust" onItemDataBound="ShowOrders">
<
ItemTemplate>
<
h3><%# ((DataRowView)Container.DataItem)["CompanyName"] %> </h3>
<
asp:Repeater id="RepOrders" runat="server">
<
HeaderTemplate>
<
table border="1">
<
tr>
<
td>Order Date</td>
<
td>Required Date</td>
<
td>Shipped Date</td>
<
td>Freight</td>
</
tr>
</
HeaderTemplate>
<
ItemTemplate>
<
tr>
<
td><%# ((DataRowView)Container.DataItem)["OrderDate"]%></td>
<
td><%# ((DataRowView)Container.DataItem)["RequiredDate"]%></td>
<
td><%# ((DataRowView)Container.DataItem)["ShippedDate"]%></td>
<
td><%# DataBinder.Eval(Container.DataItem, "Freight", "{0:c}")%></td>
</
tr></ItemTemplate>
<
FooterTemplate>
</
table>
</
FooterTemplate>
</
asp:Repeater>
</
ItemTemplate>
</
asp:Repeater>
</
body>

NOTE:
This article is for purely educational purposes. This article is entirely original, unless specified. Any resemblance to other material is an un-intentional coincidence and should not be misconstrued as malicious, slanderous, or any anything else hereof.

Login to add your contents and source code to this article
Article Extensions
Contents added by marv shine on Jun 02, 2011
share this article :
post comment
 

Getting this error while trying this example

Posted by chanchal laddha Aug 10, 2010

this works for me too.. Great work

Posted by Suchita Manandhar Jul 29, 2010

Thanks a lot!

Posted by Oren Kl Jul 08, 2009

Hey, it dont work whit me

Posted by Leo Lassen Mar 19, 2008
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor