The last step is to display the details of the selected product in DetailsView. to complete this function, add a DetailsView to the page, set its ID attribute to ProductDetails, and create a new ObjectDataSource for it. configure ObjectDataSource to use the GetProductByProductID (productID) method of the ProductsBLL class to fill in data, and use the value of the selected item of ProductsByCategory DropDownList as the value of the productID parameter.
ASP. NET DetailsView: select to use the ProductsBLL class
ASP. NET DetailsView: Configure ObjectDataSource to use the GetProductByProductID (productID) Method
ASP. NET DetailsView: Use the value of ProductsByCategory DropDownList as the value of the productID Parameter
You can select any valid field displayed in DetailsView. I decided not to display the ProductID, SupplierID, and CategoryID fields and to re-sort and format the remaining fields. in addition, I removed the Height and Width attribute settings of the DetailsView, allowing the DetailsView to be extended to the Desired Width, so that it will display data better than limiting it to the specified size. below is all the markup language (markup)
- < 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 some time to test MasterDetailsDetails in the browser. aspx page. at first glance, everything seemed as smooth as expected, but there was a small problem. when you select a new category, ProductsByCategory DropDownList will update and display the products of the selected category, but ProductDetails DetailsView will still display the information of the previous product. detailsView should be updated when different products of the selected category are selected. in addition, if your test is thorough enough, you will find that if you constantly select a new category (for example, select Beverages in Categories DropDownList, then select Condiments, and then Confections) the selection of each category will refresh ProductDetails DetailsView.
To make this question more specific, let's look at an example. when you access this page for the first time, the Beverages category is selected and related products are listed in ProductsByCategory DropDownList. chai is the currently selected product. Its details are displayed in ProductDetails DetailsView, 18.
ASP. NET DetailsView: DetailsView displays the details of the selected product.
If you change the category option Beverages to Condiments, a message is sent back. The ProductsByCategory DropDownList is updated accordingly, but the DetailsView still displays the details of Chai.
ASP. NET DetailsView: the details of the last selected product are still displayed.
Selecting a product in the list will refresh the DetailsView as expected. If you change the product and select a new category, the DetailsView will not be refreshed again. if you select a new category instead of a product, DetailsView will be refreshed. what is going on?
This problem is caused by the time scheduling of the page lifecycle. when a page is requested, it will be displayed after a series of processing. one of these operations is to check whether the SelectParameters value of the ObjectDataSource control has changed. if the changes occur, the data bound to the ObjectDataSource by the WEB control will be refreshed. for example, when a new category is selected, ProductsByCategoryDataSource ObjectDataSource finds that its parameter value has changed, and then ProductsByCategory DropDownList rebinds to obtain the product of the selected category.
In this case, the problem occurs because the ObjectDataSources check parameter changes during the lifecycle of the page before the associated WEB control is rebound. therefore, when a new category ProductsByCategoryDataSource ObjectDataSource is selected, its parameter value is changed. however, the ObjectDataSource used by ProductDetails DetailsView has not changed, because ProductsByCategory DropDownList has not been rebound. in a later life cycle, ProductsByCategory DropDownList is rebound to its ObjectDataSource to obtain the product of the selected category. when the value of ProductsByCategory DropDownList changes, the ObjectDataSource of ProductDetails DetailsView has completed the parameter value check. therefore, DetailsView displays the previous data. figure 20 describes the interaction process.
ASP. NET DetailsView: T ProductDetails DetailsView: the value of ProductsByCategory DropDownList is changed after the ObjectDataSource check is completed.
To solve this problem, we need to explicitly re-bind ProductDetails DetailsView after the ProductsByCategory DropDownList binding. you can call the DataBind () method of ProductDetails DetailsView in the DataBound event of ProductsByCategory DropDownList. add the following event processing code to MasterDetailsDetails. post-code class of the aspx page:
- protected void ProductsByCategory_DataBound(object sender, EventArgs e)
-
- {
-
- ProductDetails.DataBind();
-
- }
-
After you explicitly call the DataBind () method of ProductDetails DetailsView, everything is normal. Figure 21 highlights how this method solves this problem.
ASP. NET DetailsView: refreshes the display of ProductDetails DetailsView in the DataBound event of ProductsByCategory DropDownList.
The DropDownList control is an ideal user interface element for Master/Slave reports (one-to-many relationship between master records and slave records ). in the previous tutorial, we saw how to use a single DropDownList to filter products for the selected category. in this tutorial, we use GridView instead of DropDownList as the product list, and use DetailsView to display the details of the selected product. the concepts discussed in this tutorial can be easily extended to data models that contain multiple one-to-many relationships, such as customers, orders, and order items. generally, in a one-to-many relationship, you can always use DropDownList to represent the "master" entity (one in one-to-many, master records of the Master/Slave relationship)
- ASP. NET 2.0 data Tutorial: Add An aspx page to a site
- ASP. NET 2.0 data Tutorial: Create a master page
- ASP. NET cross-page value passing skills
- ASP. NET 2.0 data Tutorial: add custom encoding to DAL
- ASP. NET 2.0 data Tutorial: completes the data access layer