三十三基于DataList和Repeater使用DropDownList过滤的主从报表.docx
- 文档编号:23092487
- 上传时间:2023-04-30
- 格式:DOCX
- 页数:15
- 大小:371.68KB
三十三基于DataList和Repeater使用DropDownList过滤的主从报表.docx
《三十三基于DataList和Repeater使用DropDownList过滤的主从报表.docx》由会员分享,可在线阅读,更多相关《三十三基于DataList和Repeater使用DropDownList过滤的主从报表.docx(15页珍藏版)》请在冰豆网上搜索。
三十三基于DataList和Repeater使用DropDownList过滤的主从报表
在ASP.NET2.0中操作数据之三十三:
基于DataList和Repeater使用DropDownList过滤的主/从报表
作者:
heker2007字体:
[增加 减小]类型:
转载时间:
2016-05-09 我要评论
前面已经介绍过使用DropDownList过滤的主/从报表,不过当时是基于GridView,本文算是复习一下,基于DataList和Repeater再次实现一下相同的功能。
导言
在前面的使用DropDownList过滤的主/从报表一章里我们使用GridView创建的主/从表,显示一些"主"记录.用户可以根据主记录来查看"从"(详细)的内容.主/从表在呈现一对多关系和含多列的表的信息时是一个好的选择.在前面我们已经学过如何使用GridView和DetailsView来实现.本章和后面两章我们将重新复习一下这些概念,但是主要学习使用DataList和Repeater来实现.本章我们将学习使用DropDownList包含主记录,而在DataList里显示从记录.
第一步:
增加主/从教程页
首先增加本教程会用到的文件夹(DataListRepeaterFiltering)和页.新建页的时候记得选择Site.master.
Default.aspx
FilterByDropDownList.aspx
CategoryListMaster.aspx
ProductsForCategoryDetails.aspx
CategoriesAndProducts.aspx
图1:
创建DataListRepeaterFiltering文件夹和页
然后打开Default.aspx页,将SectionLevelTutorialListing.ascx用户控件拖进来.
图2:
在Default.aspx页里增加SectionLevelTutorialListing.ascx
我们需要将主/从教程添加到sitemap里.打开Web.sitemap,将下面的标记添加到“DisplayingDatawiththeDataListandRepeater”节点后:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
title="Master/DetailReportswiththeDataListandRepeater" description="SamplesofReportsthatUsetheDataListandRepeaterControls" url="~/DataListRepeaterFiltering/Default.aspx"> title="FilterbyDrop-DownList" description="Filterresultsusingadrop-downlist." url="~/DataListRepeaterFiltering/FilterByDropDownList.aspx"/> title="Master/DetailAcrossTwoPages" description="Masterrecordsononepage,detailrecordsonanother." url="~/DataListRepeaterFiltering/CategoryListMaster.aspx"/> title="Maser/DetailonOnePage" description="Masterrecordsintheleftcolumn,detailsontheright, bothonthesamepage." url="~/DataListRepeaterFiltering/CategoriesAndProducts.aspx"/> 图3: 更新之后的SiteMap 第二步: 在DropDownList里显示Categories 我们的主/从表将在DropDownList里列出categories,然后将选择的item的product用DataList显示出来.打开DataListRepeaterFiltering文件夹里的FilterByDropDownList.aspx页,拖一个DropDownList进来.将DropDownList的ID设为Categories.在智能标签上选择选择数据源,创建一个名为CategoriesDataSource的ObjectDataSource 图4: 添加一个名为CategoriesDataSource的ObjectDataSource 使用CategoriesBLL类的GetCategories()方法配置ObjectDataSource.然后为DropDownList的text和value配置字段(分别为CategoryName和CategoryID). 图5: 配置DropDownList的Text和Value 现在DropDownList里已经列出了Categories表里记录.见图6. 图6: 完成后的DropDownList 第三步: 添加ProductsDataList 下面将选择的category关联的product列出来.添加一个DataList,创建一个名为ProductsByCategoryDataSource的ObjectDataSource.用ProductsBLL类的GetProductsByCategoryID(categoryID)来配置它.因为我们的报表是只读的,所以在INSERT,UPDATE和DELETE标签里选择None. 图7: 选择GetProductsByCategoryID(categoryID)方法 点下一步,向导会提示我们为categoryID参数选择source.将Parametersource设为Control,ControlID设为Categories. 图8: 设置categoryID参数为CategoriesDropDownList 完成上面的配置后,VisualStudio会为DataList自动生成一个ItemTemplate来显示每个字段的name和value.我们来做一些改进,只显示product的name,category,supplier,quantity和price,并在每个item之间加一个 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 DataListID="DataList1"runat="server"DataKeyField="ProductID" DataSourceID="ProductsByCategoryDataSource"EnableViewState="False"> LabelID="ProductNameLabel"runat="server" Text='<%#Eval("ProductName")%>'/> LabelID="CategoryNameLabel"runat="server" Text='<%#Eval("CategoryName")%>'/> LabelID="SupplierNameLabel"runat="server" Text='<%#Eval("SupplierName")%>'/> LabelID="QuantityPerUnitLabel"runat="server" Text='<%#Eval("QuantityPerUnit")%>'/> LabelID="UnitPriceLabel"runat="server" Text='<%#Eval("UnitPrice","{0: C}")%>'/>
元素(SeoaratorTemplate).我们将使用DataList和Repeater来显示数据的ItemTemplate例子.ObjectDataSource的标记语言应该和下面差不多:
DataList>
ObjectDataSourceID="ProductsByCategoryDataSource"runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetProductsByCategoryID"TypeName="ProductsBLL"> ControlParameterControlID="Categories"Name="categoryID" PropertyName="SelectedValue"Type="Int32"/> ObjectDataSource> 在浏览器里看一下页面.第一次访问时,和Beverager关联的product都显示出来了(图9),但是改变DropDownList不会更新数据,这是因为还更新DataList需要postback.我们将DropDownList的AutoPostBack属性设为true. 图9: 第一次访问时,显示Beverage的Products 图10: 选择一个新的category(Produce),更新DataList 添加一个“--ChooseaCategory--”ListItem第一次访问页面时,Beveages默认被选中,并且在DataList里显示它的product.在使用DropDownList过滤的主/从报表里我们添加了“--ChooseaCategory--”选项(默认项),显示所有的product.在GridView里显示product时这样很方便.而对DataList而言,每个product要占很大一块屏幕,因此在选择“--ChooseaCategory--”时底下将不显示product.在DropDownList的属性里选择Items属性,添加一个Text为“--ChooseaCategory--”,Value为0的项. 图11: 添加“--ChooseaCategory--”项 你也可以直接在DropDownList的标记语言里添加以下代码: ? 1 2 3 4 5 6 7 DropDownListID="categories"runat="server"AutoPostBack="True" DataSourceID="CategoriesDataSource"DataTextField="CategoryName" DataValueField="CategoryID"EnableViewState="False"> ListItemValue="0">--ChooseaCategory-- ListItem> DropDownList> 另外我们需要将DropDownList的AppendDataBoundItems设为true.因为如果为false(默认),当categories绑定到DropDownList时将覆盖手工添加的listitem. 图12: SettheAppendDataBoundItemsPropertytoTrue 我们将“--ChooseaCategory--”的value设为0是因为系统里没有categories的value为0,因此当选择这条category时不会有product返回.浏览一下网页来确认这点.见图13. 图13: 选中“--ChooseaCategory--”时,没有Products被显示 如果你想在选择“--ChooseaCategory--”时显示所有的product,将它的value设为1.细心的读者会记起来在使用DropDownList过滤的主/从报表里我们更新了ProductsBLL类的GetProductsByCategoryID(categoryID)方法,如果categoryID为1时所有的product记录会被返回. 总结 当显示层次关系的数据时,使用主/从表来展示数据很有帮助.用户可以通过它从最高层的数据开始,逐渐进入最细节的数据.在本章我们学习了一个简单的主/从表来显示选中的category下的product.我们用DropDownList列出dategory,DataList来显示product.在下章我们将学习将主/从记录分开到两个页面.在第一个页里,显示所有的"主"记录,并有一个链接到"从"信息的link.点这个link用户会看到显示细节信息的页.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 三十三 基于 DataList Repeater 使用 DropDownList 过滤 主从 报表