|
|
|
|
|
|
Author Rank:
|
|
Technologies:
.NET 1.0/1.1, Controls,Visual C# .NET
|
|
Total downloads :
|
|
|
Total page views :
|
32621
|
|
Rating :
|
|
1/5
|
|
This article has been rated :
|
1 times
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
Related EbooksTop Videos
|
|
|
Description
|
|
LINQ for Visual C# 2005 is a short yet comprehensive guide to the major features of LINQ. It thoroughly covers LINQ to Objects, LINQ to SQL, LINQ to DataSet, and LINQ to XML.
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
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.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or
application via a range of API's. Learn More about our API connections.
|
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other
Visual Studio release. Work more productively and collaboratively-with
greater control over your work at every step. The Beta 2 can give you a
head start on achieving efficiency.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|