| Main ASP .NET Tutorial Index |
Scott Mitchell June 2006 Download the ASPNET_Data_Tutorial_7_CS.exe sample code. Contents of Tutorial 7 (Visual C#)Introduction IntroductionA common type of report is the master/detail report, in which the report begins by showing some set of "master" records. The user can then drill down into one of the master records, thereby viewing that master record's "details." Master/detail reports are an ideal choice for visualizing one-to-many relationships, such as a report showing all of the categories and then allowing a user to select a particular category and display its associated products. Additionally, master/detail reports are useful for displaying detailed information from particularly "wide" tables (ones that have a lot of columns). For example, the "master" level of a master/detail report might show just the product name and unit price of the products in the database, and drilling down into a particular product would show the additional product fields (category, supplier, quantity per unit, and so on). There are many ways with which a master/detail report can be implemented. Over this and the next three tutorials we'll look at a variety of master/detail reports. In this tutorial we'll see how to display the master records in a DropDownList control and the details of the selected list item in a GridView. In particular, this tutorial's master/detail report will list category and product information. Step 1: Displaying the Categories in a DropDownListOur master/detail report will list the categories in a DropDownList, with the selected list item's products displayed further down in the page in a GridView. The first task ahead of us, then, is to have the categories displayed in a DropDownList. Open the
Figure 1. Specify the DropDownList's Data Source Choose to add a new ObjectDataSource named
Figure 2. Add a New ObjectDataSource Named
Figure 3. Choose to Use the
Figure 4. Configure the ObjectDataSource to Use the After configuring the ObjectDataSource we still need to specify what data source field should be displayed in DropDownList and which one should be associated as the value for the list item. Have the
Figure 5. Have the DropDownList Display the At this point we have a DropDownList control that's populated with the records from the
Figure 6. A Drop-Down Lists the Current Categories Step 2: Adding the Products GridViewThat last step in our master/detail report is to list the products associated with the selected category. To accomplish this, add a GridView to the page and create a new ObjectDataSource named
Figure 7. Select the After choosing this method, the ObjectDataSource wizard prompts us for the value for the method's
Figure 8. Set the Take a moment to check out our progress in a browser. When first visiting the page, those products belong to the selected category (Beverages) are displayed (as shown in Figure 9), but changing the DropDownList doesn't update the data. This is because a postback must occur for the GridView to update. To accomplish this we have two options (neither of which requires writing any code):
Figures 9 and 10 illustrate the master/detail report in action.
Figure 9. When First Visiting the Page, the Beverage Products are Displayed
Figure 10. Selecting a New Product (Produce) Automatically Causes a PostBack, Updating the GridView Adding a "-- Choose a Category --" List ItemWhen first visiting the To add a new list item to the DropDownList, go to the Properties window and click on the ellipses in the
Figure 11. Add a "-- Choose a Category --" List Item Alternatively, you can add the list item by adding the following markup to the DropDownList: <asp:DropDownList ID="categories" runat="server" AutoPostBack="True" DataSourceID="categoriesDataSource"
DataTextField="CategoryName" DataValueField="CategoryID" EnableViewState="False">
<asp:ListItem Value="-1">-- Choose a Category --</asp:ListItem>
</asp:DropDownList>
Additionally, we need to set the DropDownList control's
Figure 12. Set the After these changes, when first visiting the page the "-- Choose a Category --" option is selected and no products are displayed.
Figure 13. On the Initial Page Load No Products Are Displayed The reason no products are displayed when because the "-- Choose a Category --" list item is selected is because its value is public Northwind.ProductsDataTable GetProductsByCategoryID(int categoryID)
{
if (categoryID < 0)
return GetProducts();
else
return Adapter.GetProductsByCategoryID(categoryID);
}
The technique used here is similar to the approach we used to display all suppliers back in the Declarative Parameters tutorial, although for this example we're using a value of Figure 14 shows a screen shot of
Figure 14. All of the Products Are Now Listed By Default SummaryWhen displaying hierarchically-related data, it often helps to present the data using master/detail reports, from which the user can start perusing the data from the top of the hierarchy and drill down into details. In this tutorial we examined building a simple master/detail report showing a selected category's products. This was accomplished by using a DropDownList for the list of categories and a GridView for the products belonging to the selected category. In the next tutorial we'll take the DropDownList interface one step further, using two DropDownLists. Happy Programming!
About the Author Scott Mitchell, author of six ASP/ASP.NET books and founder of 4GuysFromRolla.com, has been working with Microsoft Web technologies since 1998. Scott works as an independent consultant, trainer, and writer, recently completing his latest book, Sams Teach Yourself ASP.NET 2.0 in 24 Hours. He can be reached at mitchell@4guysfromrolla.com or via his blog, which can be found at http://ScottOnWriting.NET. |
||||














