Library tutorials & articles
Creating a Master-detail page
- Introduction
- Getting Started
- The Master Detail Code
The Master Detail Code
The following are things we need to create in the codebehind
- An event handler for
Page_Initevent. - An event handler for
Page_Loadevent. - An event handler for the '
Categories' repeater'sItemDataBoundevent.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MasterDetail
{
/// <summary>
/// Summary description for MasterDetail.
/// </summary>
public class MasterDetail : Page
{
public Repeater Categories;
public Repeater Products;
private void Page_Init(object sender, EventArgs e)
{
Categories.ItemDataBound += new RepeaterItemEventHandler(Categories_OnItemDataBound);
}
Nothing magic here, just setting up the base and defining the Page_Init event handler. We don't have to subscribe to the ItemDataBound event in the codebehind, we could have done it in the .aspx file by adding the event handler to the 'Categories' repeater's OnItemDataBound property.
The next thing on the agenda now is to add an event handler for Page_Init event. This method will contain the meat of the page, as that is where we fill our dataset, databind our 'Categories' repeater control and add a relationship between our two tables: private void Page_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection("Server=localhost; User Id=sa; Password=; Initial Catalog=Northwind");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
// Fill dataset
da.SelectCommand = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories", connection);
da.Fill(ds, "Categories");
da.SelectCommand = new SqlCommand("SELECT CategoryID, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock FROM Products", connection);
da.Fill(ds, "Products");
// We need to define a relationship between the two tables in the dataset
DataRelation relation = new DataRelation("categoryId", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]);
ds.Relations.Add(relation);
Categories.DataSource = ds.Tables["Categories"].DefaultView;
Categories.DataBind();
}
Briefly all we do here is fill up our dataset with the two tables, and add a relationship between the two tables. The way you define a relationship between two tables is by creating an instance of the DataRelation class, and then you add it to the dataset's Relations collection by using the collection's Add method. Instead of doing like I did above you could have done the following instead:
ds.Relations.Add( new DataRelation("categoryId", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]) );
Although I prefer the first version as it looks nicer ;)
All that is left now is to add the event handler for Categories_ItemDataBound event: private void Categories_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((Repeater) e.Item.FindControl("Products")).DataSource = ((DataRowView) e.Item.DataItem).CreateChildView("categoryId");
((Repeater) e.Item.FindControl("Products")).DataBind();
}
}
}
}
In the method we check to see if the item that is databound is actually either an Item or an AlternatingItem . We have to do this because the objects we want to toy around with are only contained inside the ItemTemplate (in this case we could actually remove the AlternatingItem check, but I added it to make a point). Through the usage of the FindControl method and type casting we can dynamically alter the 'Products' repeater control's datasource for each category iteration.
Well that's it, compile it and have fun!
Related articles
Related discussion
-
Using FedEx Web Service to Calculcate Shipping Cost
by bhora123 (4 replies)
-
Very Urgent regarding deleting the images from a folder
by rameshbandi (2 replies)
-
Dynamically Generating PDFs in .NET
by nike12 (10 replies)
-
New style of Javascript used in extenders.
by mittalpa (0 replies)
-
Not able to launch the web application
by NaseemAhmed (0 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
I am trying to test your code, so i found this below error:
First all:I only put the Code into a web application folder,not user the vs.net to do,but when i try to browse the page,the application error tell me
Second:I created a solution in vs.net,above the error is losed,but the .aspx page not display the result,why?
My sql server connection string is right!
This thread is for discussions of Creating a Master-detail page.