Use ASP. NET DetailsView to display details

Source: Internet
Author: User

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)

 
 
  1. < asp:DetailsView ID="ProductDetails" runat="server" AutoGenerateRows="False" DataKeyNames="ProductID" 
  2.  
  3. DataSourceID="ObjectDataSource1" EnableViewState="False">  
  4.  
  5. < Fields>  
  6.  
  7. < asp:BoundField DataField="ProductName" HeaderText="Product" SortExpression="ProductName" />  
  8.  
  9. < asp:BoundField DataField="CategoryName" HeaderText="Category" ReadOnly="True" SortExpression="CategoryName" />  
  10.  
  11. < asp:BoundField DataField="SupplierName" HeaderText="Supplier" ReadOnly="True" SortExpression="SupplierName" />  
  12.  
  13. < asp:BoundField DataField="QuantityPerUnit" HeaderText="Qty/Unit" SortExpression="QuantityPerUnit" />  
  14.  
  15. < asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="Price" 
  16.  
  17. HtmlEncode="False" SortExpression="UnitPrice" />  
  18.  
  19. < asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="Units In Stock" />  
  20.  
  21. < asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" SortExpression="Units On Order" />  
  22.  
  23. < asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" SortExpression="Reorder Level" />  
  24.  
  25. < asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" />  
  26.  
  27. < /Fields>  
  28.  
  29. < /asp:DetailsView>  
  30.  

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:

 
 
  1. protected void ProductsByCategory_DataBound(object sender, EventArgs e)  
  2.  
  3. {  
  4.  
  5. ProductDetails.DataBind();  
  6.  
  7. }  
  8.  

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)

  1. ASP. NET 2.0 data Tutorial: Add An aspx page to a site
  2. ASP. NET 2.0 data Tutorial: Create a master page
  3. ASP. NET cross-page value passing skills
  4. ASP. NET 2.0 data Tutorial: add custom encoding to DAL
  5. ASP. NET 2.0 data Tutorial: completes the data access layer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.