Blue Theme Orange Theme Green Theme Red Theme
 
Mindcracker MVP Summit 2012
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » WebForms Controls » Master-Detail Display using Nested Repeater Web Forms Control

Master-Detail Display using Nested Repeater Web Forms Control

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.

Author Rank :
Page Views : 48766
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Chart
Become a Sponsor
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by marv shine on Jun 02, 2011
 [Top] Rate this article
 
 About the author
 
Dipal Choksi
Dipal Choksi has over 10 years of industry experience in team-effort projects and also as an individual contributor. She has been working on the .Net platform since the beta releases of .Net 1.0.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
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!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Dont works by Leo On March 19, 2008
Hey, it dont work whit me
Reply | Email | Modify 
Exactly what I needed by Oren On July 8, 2009
Thanks a lot!
Reply | Email | Modify 
thanks by Suchita On July 29, 2010
this works for me too.. Great work
Reply | Email | Modify 
Column argument cannot be null by chanchal On August 10, 2010
Getting this error while trying this example
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.