Ç°ÃæÒѾ½éÉܹýʹÓÃ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
ÎÒÃÇÐèÒª½«Ö÷/´Ó½Ì³ÌÌí¼Óµ½site mapÀï.´ò¿ªWeb.sitemap,½«ÏÂÃæµÄ±ê¼ÇÌí¼Óµ½¡°Displaying Data with the DataList and Repeater¡±½Úµãºó:
<siteMapNode
title="Master/Detail Reports with the DataList and Repeater"
description="Samples of Reports that Use the DataList and Repeater Controls"
url="~/DataListRepeaterFiltering/Default.aspx">
<siteMapNode
title="Filter by Drop-Down List"
description="Filter results using a drop-down list."
url="~/DataListRepeaterFiltering/FilterByDropDownList.aspx" />
<siteMapNode
title="Master/Detail Across Two Pages"
description="Master records on one page, detail records on another."
url="~/DataListRepeaterFiltering/CategoryListMaster.aspx" />
<siteMapNode
title="Maser/Detail on One Page"
description="Master records in the left column, details on the right,
both on the same page."
url="~/DataListRepeaterFiltering/CategoriesAndProducts.aspx" />
</siteMapNode>
ͼ 3: ¸üÐÂÖ®ºóµÄSite Map
µÚ¶þ²½: ÔÚ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
µÚÈý²½: Ìí¼ÓProducts DataList
¡¡¡¡ÏÂÃ潫ѡÔñµÄcategory¹ØÁªµÄproductÁгöÀ´.Ìí¼ÓÒ»¸öDataList,´´½¨Ò»¸öÃûΪProductsByCategoryDataSourceµÄObjectDataSource.ÓÃProductsBLLÀàµÄGetProductsByCategoryID(categoryID)À´ÅäÖÃËü.ÒòΪÎÒÃǵı¨±íÊÇÖ»¶ÁµÄ,ËùÒÔÔÚINSERT,UPDATEºÍDELETE±êÇ©ÀïÑ¡ÔñNone.
ͼ 7: Ñ¡ÔñGetProductsByCategoryID(categoryID)·½·¨
µãÏÂÒ»²½,Ïòµ¼»áÌáʾÎÒÃÇΪcategoryID²ÎÊýÑ¡Ôñsource.½«Parameter sourceÉèΪControl,ControlIDÉèΪCategories.
ͼ 8: ÉèÖÃcategoryID²ÎÊýΪCategories DropDownList
¡¡¡¡Íê³ÉÉÏÃæµÄÅäÖúó,Visual Studio»áΪDataList×Ô¶¯Éú³ÉÒ»¸öItemTemplateÀ´ÏÔʾÿ¸ö×ֶεÄnameºÍvalue.ÎÒÃÇÀ´×öһЩ¸Ä½ø,Ö»ÏÔʾproductµÄname,category,supplier,quantityºÍprice,²¢ÔÚÿ¸öitemÖ®¼ä¼ÓÒ»¸ö<hr>ÔªËØ(SeoaratorTemplate).ÎÒÃǽ«Ê¹ÓÃDataListºÍRepeaterÀ´ÏÔʾÊý¾Ý µÄItemTemplateÀý×Ó.ObjectDataSourceµÄ±ê¼ÇÓïÑÔÓ¦¸ÃºÍÏÂÃæ²î²»¶à:
<asp:DataList ID="DataList1" runat="server" DataKeyField="ProductID"
DataSourceID="ProductsByCategoryDataSource" EnableViewState="False">
<ItemTemplate>
<h4>
<asp:Label ID="ProductNameLabel" runat="server"
Text='<%# Eval("ProductName") %>' />
</h4>
<table border="0">
<tr>
<td class="ProductPropertyLabel">Category:</td>
<td><asp:Label ID="CategoryNameLabel" runat="server"
Text='<%# Eval("CategoryName") %>' /></td>
<td class="ProductPropertyLabel">Supplier:</td>
<td><asp:Label ID="SupplierNameLabel" runat="server"
Text='<%# Eval("SupplierName") %>' /></td>
</tr>
<tr>
<td class="ProductPropertyLabel">Qty/Unit:</td>
<td><asp:Label ID="QuantityPerUnitLabel" runat="server"
Text='<%# Eval("QuantityPerUnit") %>' /></td>
<td class="ProductPropertyLabel">Price:</td>
<td><asp:Label ID="UnitPriceLabel" runat="server"
Text='<%# Eval("UnitPrice", "{0:C}") %>' /></td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
<asp:ObjectDataSource ID="ProductsByCategoryDataSource" runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetProductsByCategoryID" TypeName="ProductsBLL">
<SelectParameters>
<asp:ControlParameter ControlID="Categories" Name="categoryID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
¡¡¡¡ÔÚä¯ÀÀÆ÷Àï¿´Ò»ÏÂÒ³Ãæ.µÚÒ»´Î·ÃÎÊʱ,ºÍBeverager¹ØÁªµÄproduct¶¼ÏÔʾ³öÀ´ÁË(ͼ9),µ«ÊǸıäDropDownList²»»á¸üÐÂÊý¾Ý,ÕâÊÇÒòΪ»¹¸üÐÂDataListÐèÒªpostback.ÎÒÃǽ«DropDownListµÄAutoPostBackÊôÐÔÉèΪtrue.
ͼ 9: µÚÒ»´Î·ÃÎÊʱ, ÏÔʾBeverageµÄ Products
ͼ 10: Ñ¡ÔñÒ»¸öеÄcategory(Produce),¸üÐÂDataList
¡¡¡¡Ìí¼ÓÒ»¸ö ¡°-- Choose a Category --¡± List ItemµÚÒ»´Î·ÃÎÊÒ³Ãæʱ,BeveagesĬÈϱ»Ñ¡ÖÐ,²¢ÇÒÔÚDataListÀïÏÔʾËüµÄproduct.ÔÚʹÓÃDropDownList¹ýÂ˵ÄÖ÷/´Ó±¨±í ÀïÎÒÃÇÌí¼ÓÁË¡°-- Choose a Category --¡±Ñ¡Ïî(ĬÈÏÏî),ÏÔʾËùÓеÄproduct.ÔÚGridViewÀïÏÔʾproductʱÕâÑùºÜ·½±ã.¶ø¶ÔDataList¶øÑÔ,ÿ¸öproductÒªÕ¼ºÜ´óÒ»¿éÆÁÄ»,Òò´ËÔÚÑ¡Ôñ¡°-- Choose a Category --¡±Ê±µ×Ͻ«²»ÏÔʾproduct.ÔÚDropDownListµÄÊôÐÔÀïÑ¡ÔñItemsÊôÐÔ,Ìí¼ÓÒ»¸öTextΪ¡°-- Choose a Category --¡±,ValueΪ0µÄÏî.
ͼ 11: Ìí¼Ó ¡°-- Choose a Category --¡± Ïî
ÄãÒ²¿ÉÒÔÖ±½ÓÔÚDropDownListµÄ±ê¼ÇÓïÑÔÀïÌí¼ÓÒÔÏ´úÂë:
<asp:DropDownList ID="categories" runat="server" AutoPostBack="True"
DataSourceID="CategoriesDataSource" DataTextField="CategoryName"
DataValueField="CategoryID" EnableViewState="False">
<asp:ListItem Value="0">-- Choose a Category --</asp:ListItem>
</asp:DropDownList>
¡¡¡¡ÁíÍâÎÒÃÇÐèÒª½«DropDownListµÄAppendDataBoundItemsÉèΪtrue.ÒòΪÈç¹ûΪfalse(ĬÈÏ),µ±categories°ó¶¨µ½DropDownListʱ½«¸²¸ÇÊÖ¹¤Ìí¼ÓµÄlist item.
ͼ 12: Set the AppendDataBoundItems Property to True
¡¡¡¡ÎÒÃǽ«¡°-- Choose a Category --¡± µÄvalueÉèΪ0ÊÇÒòΪϵͳÀïûÓÐcategoriesµÄvalueΪ0,Òò´Ëµ±Ñ¡ÔñÕâÌõcategoryʱ²»»áÓÐproduct·µ»Ø.ä¯ÀÀÒ»ÏÂÍøÒ³À´È·ÈÏÕâµã.¼ûͼ13.
ͼ 13: Ñ¡ÖС°-- Choose a Category --¡± ʱ, ûÓÐProducts ±»ÏÔʾ
¡¡¡¡Èç¹ûÄãÏëÔÚÑ¡Ôñ¡°-- Choose a Category --¡± ʱÏÔʾËùÓеÄproduct,½«ËüµÄvalueÉèΪ1.ϸÐĵĶÁÕß»á¼ÇÆðÀ´ÔÚʹÓÃDropDownList¹ýÂ˵ÄÖ÷/´Ó±¨±í ÀïÎÒÃǸüÐÂÁËProductsBLLÀàµÄGetProductsByCategoryID(categoryID)·½·¨,Èç¹ûcategoryIDΪ1ʱËùÓеÄproduct¼Ç¼»á±»·µ»Ø.
×ܽá
¡¡¡¡µ±ÏÔʾ²ã´Î¹ØϵµÄÊý¾Ýʱ,ʹÓÃÖ÷/´Ó±íÀ´Õ¹Ê¾Êý¾ÝºÜÓаïÖú.Óû§¿ÉÒÔͨ¹ýËü´Ó×î¸ß²ãµÄÊý¾Ý¿ªÊ¼,Öð½¥½øÈë×îϸ½ÚµÄÊý¾Ý.ÔÚ±¾ÕÂÎÒÃÇѧϰÁËÒ»¸ö¼òµ¥µÄÖ÷/´Ó±íÀ´ÏÔʾѡÖеÄcategoryϵÄproduct.ÎÒÃÇÓÃDropDownListÁгödategory,DataListÀ´ÏÔʾproduct.ÔÚÏÂÕÂÎÒÃǽ«Ñ§Ï°½«Ö÷/´Ó¼Ç¼·Ö¿ªµ½Á½¸öÒ³Ãæ.ÔÚµÚÒ»¸öÒ³Àï,ÏÔʾËùÓеÄ"Ö÷"¼Ç¼,²¢ÓÐÒ»¸öÁ´½Óµ½"´Ó"ÐÅÏ¢µÄlink.µãÕâ¸ölinkÓû§»á¿´µ½ÏÔʾϸ½ÚÐÅÏ¢µÄÒ³.
¡¡¡¡×£±à³ÌÓä¿ì!
×÷Õß¼ò½é
¡¡¡¡Scott Mitchell£¬ÖøÓÐÁù±¾ASP/ASP.NET·½ÃæµÄÊ飬ÊÇ4GuysFromRolla.comµÄ´´Ê¼ÈË£¬×Ô1998ÄêÒÔÀ´Ò»Ö±Ó¦Óà ΢ÈíWeb¼¼Êõ¡£ScottÊǸö¶ÀÁ¢µÄ¼¼Êõ×Éѯ¹ËÎÊ£¬Åàѵʦ£¬×÷¼Ò£¬×î½üÍê³ÉÁ˽«ÓÉSams³ö°æÉç³ö°æµÄÐÂ×÷£¬24СʱÄÚ¾«Í¨ASP.NET 2.0¡£ËûµÄÁªÏµµçÓÊΪmitchell@4guysfromrolla.com£¬Ò²¿ÉÒÔͨ¹ýËûµÄ²©¿Íhttp://ScottOnWriting.NETÓëËûÁªÏµ¡£ |