技术开发 频道

ASP.NET2.0:AdventureWorks贸易系统分析


3. 产品子类别显示过程


    产品子类别页面允许向用户显示所有产品子类别的列表,然后通过所有产品列表中包括的各个子类别进行导航。示例3说明了产品子类别页面的实现。

   示例3:产品子类别显示页面
<%@ Page Language="C#" MasterPageFile="~/Common.master" Title="Product Sub Category Display" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:ObjectDataSource ID="subCategorySource"
TypeName="AdventureWorksTraderBiz.ProductSubcategoryBiz"
SelectMethod="GetProductSubCategories" runat="server">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="ProductCategoryID"
Direction="Input" Name="productCategoryID" DefaultValue="1"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:Label runat="server" ID="lblHeading" Font-Size="Medium"
Font-Underline="False" ForeColor="#0000C0">
Click on the SubCategory to go to the Products
</asp:Label><br />
<br />
<asp:GridView HeaderStyle-HorizontalAlign="Center"
HeaderStyle-Font-Bold="True" HeaderStyle-BackColor="blue"
HeaderStyle-ForeColor="White" AutoGenerateColumns="False"
ID="gridSubCategories" runat="server" DataSourceID="subCategorySource">
<Columns>
<asp:BoundField ReadOnly="True" HeaderText="SubcategoryID"
DataField="ProductSubcategoryID" />
<asp:BoundField HeaderText="CategoryID" DataField="ProductCategoryID" />
<asp:HyperLinkField HeaderText="Name" DataTextField="Name"
DataNavigateUrlFields="ProductSubcategoryID"
DataNavigateUrlFormatString='"ProductDisplay.aspx?' ProductSubcategoryID="{0}" />
<asp:BoundField HeaderText='"Row' Guid" DataField="Rowguid" />
<asp:BoundField HeaderText='"Modified' Date" HtmlEncode="false" DataFormatString="{0:MM/dd/yyyy}" DataField="ModifiedDate" />
</Columns>
</asp:GridView>
</asp:Content>
    示例3包括名为subCategorySource的ObjectDataSource控件,该控件绑定了ProductSubcategoryBiz类的GetProductSubCategories()方法。正如前文讲解的那样,GetProductSubCategories()方法可接受产品类别ID为参数,同时返回属于该产品类别的所有子类别信息。为了调用这个方法,subCategorySource控件应该将产品类别ID(由产品类别显示页面返回)传递给该方法。在这种情况下,使用QueryStringParameter集合获取产品类别ID。为此,将QueryStringParameter模板的QueryStringField设置为查询字符串字段名称,同时将Name属性设置为GetProductSubcategories()方法参数的名称。这样在前面页面选中的产品类别ID则用于SQL查询的参数。开发人员还可以使用DefaultValue属性设置产品类别ID默认值为1。当首次请求页面时,将使用默认值。
0
相关文章