| Main ASP .NET Tutorial Index |
Scott Mitchell June 2006 Download the ASPNET_Data_Tutorial_8_CS.exe sample code. Contents of Tutorial 8 (Visual C#)Introduction IntroductionIn the previous tutorial we examined how to display a simple master/details report using a single DropDownList populated with the categories and a GridView showing those products that belong to the selected category. This report pattern works well when displaying records that have a one-to-many relationship and can easily be extended to work for scenarios that include multiple one-to-many relationships. For example, an order entry system would have tables that correspond to customers, orders, and order line items. A given customer may have multiple orders with each order consisting of multiple items. Such data can be presented to the user with two DropDownLists and a GridView. The first DropDownList would have a list item for each customer in the database with the second one's contents being the orders placed by the selected customer. A GridView would list the line items from the selected order. While the Northwind database include the canonical customer/order/order details information in its Step 1: Creating and Populating the Categories DropDownListOur first goal is to add the DropDownList that lists the categories. These steps were examined in detail in the preceding tutorial, but are summarized here for completeness. Open the
Figure 1. Add a New Data Source for the DropDownList The new data source should, naturally, be an ObjectDataSource. Name this new ObjectDataSource
Figure 2. Choose to Use the
Figure 3. Configure the ObjectDataSource to Use the After configuring the ObjectDataSource we still need to specify which data source field should be displayed in the
Figure 4. Have the DropDownList Display the At this point we have a DropDownList control (
Figure 5. Enable AutoPostBack for the Step 2: Displaying the Selected Category's Products in a Second DropDownListWith the
Figure 6. Add a New Data Source for the
Figure 7. Create a New ObjectDataSource Named Since the
Figure 8. Choose to Use the
Figure 9. Configure the ObjectDataSource to Use the In the final step of the wizard we need to specify the value of the
Figure 10. Pull the With the ObjectDataSource configured, all that remains is to specify what data source fields are used for the display and value of the DropDownList's items. Display the
Figure 11. Specify the Data Source Fields Used for the DropDownList's With the ObjectDataSource and
Figure 12. When First Visiting the Page, the Beverages Category Is Selected
Figure 13. Choosing a Different Category Displays the New Category's Products Currently the
Figure 14. Enable the AutoPostBack Feature for the Step 3: Using a DetailsView to Display Details for the Selected ProductThe final step is to display the details for the selected product in a DetailsView. To accomplish this, add a DetailsView to the page, set its
Figure 15. Choose to Use the
Figure 16. Configure the ObjectDataSource to Use the
Figure 17. Pull the You can choose to display any of the available fields in the DetailsView. I've opted to remove the <asp:DetailsView ID="ProductDetails" runat="server" AutoGenerateRows="False" DataKeyNames="ProductID"
DataSourceID="ObjectDataSource1" EnableViewState="False">
<Fields>
<asp:BoundField DataField="ProductName" HeaderText="Product" SortExpression="ProductName" />
<asp:BoundField DataField="CategoryName" HeaderText="Category" ReadOnly="True" SortExpression="CategoryName" />
<asp:BoundField DataField="SupplierName" HeaderText="Supplier" ReadOnly="True" SortExpression="SupplierName" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="Qty/Unit" SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="Price"
HtmlEncode="False" SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="Units In Stock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" SortExpression="Units On Order" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" SortExpression="Reorder Level" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" />
</Fields>
</asp:DetailsView>
Take a moment to try out the To help concretize this problem, let's look at a specific example. When you first visit the page the Beverages category is selected and the related products are loaded in the
Figure 18. The Selected Product's Details are Displayed in a DetailsView If you change the category selection from Beverages to Condiments, a postback occurs and the
Figure 19. The Previously Selected Product's Details Are Still Displayed Picking a new product from the list refreshes the DetailsView as expected. If you pick a new category after changing the product, the DetailsView again won't refresh. However, if instead of choosing a new product you selected a new category, the DetailsView would refresh. What in the world is going on here? The problem is a timing issue in the page's lifecycle. Whenever a page is requested it proceeds through a number of steps as its rendering. In one of these steps the ObjectDataSource controls check to see if any of their The problem that arises in this situation is that the point in the page lifecycle that the ObjectDataSources check for changed parameters occurs before the rebinding of the associated data Web controls. Therefore, when selecting a new category the
Figure 20. The To remedy this we need to explicitly rebind the protected void ProductsByCategory_DataBound(object sender, EventArgs e)
{
ProductDetails.DataBind();
}
After this explicit call to the
Figure 21. The SummaryThe DropDownList serves as an ideal user interface element for master/detail reports where there is a one-to-many relationship between the master and detail records. In the preceding tutorial we saw how to use a single DropDownList to filter the products displayed by the selected category. In this tutorial we replaced the GridView of products with a DropDownList, and used a DetailsView to display the details of the selected product. The concepts discussed in this tutorial can easily be extended to data models involving multiple one-to-many relationships, such as customers, orders, and order items. In general, you can always add a DropDownList for each of the "one" entities in the one-to-many relationships. 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. Special Thanks To...This tutorial series was reviewed by many helpful reviewers. Lead reviewer for this tutorial was Hilton Giesenow. Interested in reviewing my upcoming MSDN articles? If so, drop me a line at mitchell@4GuysFromRolla.com. |
||||





















