收藏 分销(赏)

外文翻译-英文文献-使用两个DropDownList过滤的主从报表.doc

上传人:可**** 文档编号:3559085 上传时间:2024-07-09 格式:DOC 页数:30 大小:2.02MB
下载 相关 举报
外文翻译-英文文献-使用两个DropDownList过滤的主从报表.doc_第1页
第1页 / 共30页
外文翻译-英文文献-使用两个DropDownList过滤的主从报表.doc_第2页
第2页 / 共30页
外文翻译-英文文献-使用两个DropDownList过滤的主从报表.doc_第3页
第3页 / 共30页
外文翻译-英文文献-使用两个DropDownList过滤的主从报表.doc_第4页
第4页 / 共30页
外文翻译-英文文献-使用两个DropDownList过滤的主从报表.doc_第5页
第5页 / 共30页
点击查看更多>>
资源描述

1、 Master/Detail Filtering With Two DropDownListsIntroductionIn 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 work

2、s 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

3、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 ones contents being the orders placed by the selected custo

4、mer. 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 Customers, Orders, and Order Details tables, these tables arent captured in our architecture. Nonetheless, we can still illustrate usin

5、g two dependent DropDownLists. The first DropDownList will list the categories and the second the products belonging to the selected category. A DetailsView will then list the details of the selected product.Step 1: Creating and Populating the Categories DropDownListOur first goal is to add the Drop

6、DownList that lists the categories. These steps were examined in detail in the preceding tutorial, but are summarized here for completeness. Open the MasterDetailsDetails.aspx page in the Filtering folder, add a DropDownList to the page, set its ID property to Categories, and then click the Configur

7、e Data Source link in its smart tag. From the Data Source Configuration Wizard choose to add a new data source.Figure 1: Add a New Data Source for the DropDownList (Click to view full-size image)The new data source should, naturally, be an ObjectDataSource. Name this new ObjectDataSource CategoriesD

8、ataSource and have it invoke the CategoriesBLL objects GetCategories() method.Figure 2: Choose to Use the CategoriesBLL Class (Click to view full-size image)Figure 3: Configure the ObjectDataSource to Use the GetCategories() Method (Click to view full-size image)After configuring the ObjectDataSourc

9、e we still need to specify which data source field should be displayed in the Categories DropDownList and which one should be configured as the value for the list item. Set the CategoryName field as the display and CategoryID as the value for each list item.Figure 4: Have the DropDownList Display th

10、e CategoryName Field and Use CategoryID as the Value (Click to view full-size image)At this point we have a DropDownList control (Categories) thats populated with the records from the Categories table. When the user chooses a new category from the DropDownList well want a postback to occur in order

11、to refresh the product DropDownList that were going to create in Step 2. Therefore, check the Enable AutoPostBack option from the categories DropDownLists smart tag.Figure 5: Enable AutoPostBack for the Categories DropDownList (Click to view full-size image)Step 2: Displaying the Selected Categorys

12、Products in a Second DropDownListWith the Categories DropDownList completed, our next step is to display a DropDownList of products belonging to the selected category. To accomplish this, add another DropDownList to the page named ProductsByCategory. As with the Categories DropDownList, create a new

13、 ObjectDataSource for the ProductsByCategory DropDownList named ProductsByCategoryDataSource.Figure 6: Add a New Data Source for the ProductsByCategory DropDownList (Click to view full-size image)Figure 7: Create a New ObjectDataSource Named ProductsByCategoryDataSource (Click to view full-size imag

14、e)Since the ProductsByCategory DropDownList needs to display just those products belonging to the selected category, have the ObjectDataSource invoke the GetProductsByCategoryID(categoryID) method from the ProductsBLL object.Figure 8: Choose to Use the ProductsBLL Class (Click to view full-size imag

15、e)Figure 9: Configure the ObjectDataSource to Use the GetProductsByCategoryID(categoryID) Method (Click to view full-size image)In the final step of the wizard we need to specify the value of the categoryID parameter. Assign this parameter to the selected item from the Categories DropDownList.Figure

16、 10: Pull the categoryID Parameter Value from the Categories DropDownList (Click to view full-size image)With the ObjectDataSource configured, all that remains is to specify what data source fields are used for the display and value of the DropDownLists items. Display the ProductName field and use t

17、he ProductID field as the value.Figure 11: Specify the Data Source Fields Used for the DropDownLists ListItems Text and Value Properties (Click to view full-size image)With the ObjectDataSource and ProductsByCategory DropDownList configured our page will display two DropDownLists: the first will lis

18、t all of the categories while the second will list those products belonging to the selected category. When the user selects a new category from the first DropDownList, a postback will ensue and the second DropDownList will be rebound, showing those products that belong to the newly selected category

19、. Figures 12 and 13 show MasterDetailsDetails.aspx in action when viewed through a browser.Figure 12: When First Visiting the Page, the Beverages Category is Selected (Click to view full-size image)Figure 13: Choosing a Different Category Displays the New Categorys Products (Click to view full-size

20、image)Currently the productsByCategory DropDownList, when changed, does not cause a postback. However, we will want a postback to occur once we add a DetailsView to display the selected products details (Step 3). Therefore, check the Enable AutoPostBack checkbox from the productsByCategory DropDownL

21、ists smart tag.Figure 14: Enable the AutoPostBack Feature for the productsByCategory DropDownList (Click to view full-size image)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 th

22、is, add a DetailsView to the page, set its ID property to ProductDetails, and create a new ObjectDataSource for it. Configure this ObjectDataSource to pull its data from the ProductsBLL classs GetProductByProductID(productID) method using the selected value of the ProductsByCategory DropDownList for

23、 the value of the productID parameter.Figure 15: Choose to Use the ProductsBLL Class (Click to view full-size image)Figure 16: Configure the ObjectDataSource to Use the GetProductByProductID(productID) Method (Click to view full-size image)Figure 17: Pull the productID Parameter Value from the Produ

24、ctsByCategory DropDownList (Click to view full-size image)You can choose to display any of the available fields in the DetailsView. Ive opted to remove the ProductID, SupplierID, and CategoryID fields and reordered and formatted the remaining fields. In addition, I cleared out the DetailsViews Heigh

25、t and Width properties, allowing the DetailsView to expand to the width needed to best display its data rather than having it constrained to a specified size. The full markup appears below: Take a moment to try out the MasterDetailsDetails.aspx page in a browser. At first glance it may appear that e

26、verything is working as desired, but theres a subtle problem. When you choose a new category the ProductsByCategory DropDownList is updated to include those products for the selected category, but the ProductDetails DetailsView continued to show the previous product information. The DetailsView is u

27、pdated when choosing a different product for the selected category. Furthermore, if you test thoroughly enough, youll find that if you continually choose new categories (such as choosing Beverages from the Categories DropDownList, then Condiments, then Confections) every other category selection cau

28、ses the ProductDetails DetailsView to be refreshed.To help concretize this problem, lets look at a specific example. When you first visit the page the Beverages category is selected and the related products are loaded in the ProductsByCategory DropDownList. Chai is the selected product and its detai

29、ls are displayed in the ProductDetails DetailsView, as shown in Figure 18.Figure 18: The Selected Products Details are Displayed in a DetailsView (Click to view full-size image)If you change the category selection from Beverages to Condiments, a postback occurs and the ProductsByCategory DropDownLis

30、t is updated accordingly, but the DetailsView still displays details for Chai.Figure 19: The Previously Selected Products Details are Still Displayed (Click to view full-size image)Picking a new product from the list refreshes the DetailsView as expected. If you pick a new category after changing th

31、e product, the DetailsView again wont 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 pages lifecycle. Whenever a page is requested it proceeds through a number

32、of steps as its rendering. In one of these steps the ObjectDataSource controls check to see if any of their SelectParameters values have changed. If so, the data Web control bound to the ObjectDataSource knows that it needs to refresh its display. For example, when a new category is selected, the Pr

33、oductsByCategoryDataSource ObjectDataSource detects that its parameter values have changed and the ProductsByCategory DropDownList rebinds itself, getting the products for the selected category.The problem that arises in this situation is that the point in the page lifecycle that the ObjectDataSourc

34、es check for changed parameters occurs before the rebinding of the associated data Web controls. Therefore, when selecting a new category the ProductsByCategoryDataSource ObjectDataSource detects a change in its parameters value. The ObjectDataSource used by the ProductDetails DetailsView, however,

35、doesnt note any such changes because the ProductsByCategory DropDownList has yet to be rebound. Later in the lifecycle the ProductsByCategory DropDownList rebinds to its ObjectDataSource, grabbing the products for the newly selected category. While the ProductsByCategory DropDownLists value has chan

36、ged, the ProductDetails DetailsViews ObjectDataSource has already done its parameter value check; therefore, the DetailsView displays its previous results. This interaction is depicted in Figure 20.Figure 20: The ProductsByCategory DropDownList Value Changes After the ProductDetails DetailsViews Obj

37、ectDataSource Checks for Changes (Click to view full-size image)To remedy this we need to explicitly rebind the ProductDetails DetailsView after the ProductsByCategory DropDownList has been bound. We can accomplish this by calling the ProductDetails DetailsViews DataBind() method when the ProductsBy

38、Category DropDownLists DataBound event fires. Add the following event handler code to the MasterDetailsDetails.aspx pages code-behind class (refer to the Programmatically Setting the ObjectDataSources Parameter Values for a discussion on how to add an event handler):protected void ProductsByCategory

39、_DataBound(object sender, EventArgs e) ProductDetails.DataBind(); After this explicit call to the ProductDetails DetailsViews DataBind() method has been added, the tutorial works as expected. Figure 21 highlights how this changed remedied our earlier problem.Figure 21: The ProductDetails DetailsView

40、 is Explicitly Refreshed When the ProductsByCategory DropDownLists DataBound Event Fires (Click to view full-size image)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

41、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 tutor

42、ial 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 AuthorScott Mitchell, author of

43、seven ASP/ASP.NET books and founder of 4GuysFromR, has been working with Microsoft Web technologies since 1998. Scott works as an independent consultant, trainer, and writer. His latest book is Sams Teach Yourself ASP.NET 2.0 in 24 Hours. He can be reached at mitchell4GuysFromR. or via his blog, whi

44、ch can be found at http:/ScottOnWriting.NET.The article is from part of Working with Data in ASP.NET 2.0.Site:使用两个DropDownList过滤的主/从报表导言在前面的指南中我们研究了如何显示一个简单的主/从报表,该报表使用DropDownList和GridView控件,DropDownList填充类别,GridView显示选定类别的产品。 这类报表用于显示具有一对多关系的记录时非常合适,同时它也可以很容易的被扩展以显示多个一对多关系的数据。比如,一个订单系统应该包含表示客户,订单和

45、订单明细的表。一个客户也许有多个订单,每个订单又包含多条订单项。这样的数据可以使用两个DropDownList和一个GridView呈现给用户。第一个DropDownList应该包含数据库中所有客户的列表,第二个DropDownList的内容是选定客户的订单。 GridView用于列出所选定订单的订单明细项。第一步:创建DropDownList并使用类别数据填充 我们的第一个目标是添加一个能够列出类别的DropDownList。这些步骤在前面的指南中已经做详细的分析,但为了保持本篇指南的完整性有必要在这里简单概括一下。打开Filtering文件夹中的MasterDetailsDetails.aspx,在页面上添加一个DropDownList

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服