|
<%@ Master Language="" %> |
母版页封装了Web站点所有页面的页头和左边导航信息。既然读者已经了解了母版页,下面讲解提供核心功能的内容页。首先,讨论产品类别显示过程。
2. 产品类别显示过程
在产品类别显示过程中,用户可浏览AdventureWorks数据库中的类别列表。另外,用户还可以浏览属于一个产品类别的产品子类别。通过单击产品类别名称,用户能够访问产品子类别。实例2列举了页面的实现代码。
示例2:实现继承自母版页的产品类别显示页面
| <%@ Page Language="" MasterPageFile="~/Common.master" Title="Product Categories Display" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <asp:ObjectDataSource ID="categorySource" EnableCaching="true" SqlCacheDependency="CommandNotification" CacheDuration="Infinite" TypeName="AdventureWorksTraderBiz.ProductCategoryBiz" SelectMethod="GetProductCategories" runat="server"> </asp:ObjectDataSource> <asp:Label runat="server" ID="lblHeading" Font-Size="Medium" Font-Underline="False" ForeColor="#0000C0"> Click on the Category to go to the SubCategories </asp:Label><br /> <br /> <asp:GridView HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White" AutoGenerateColumns="False" ID="gridCategories" runat="server" DataSourceID="categorySource"> <Columns> <asp:BoundField ReadOnly="True" HeaderText="CategoryID" DataField="ProductCategoryID" /> <asp:HyperLinkField HeaderText="Name" DataTextField="Name" DataNavigateUrlFields="ProductCategoryID" DataNavigateUrlFormatString="ProductSubcategoryDisplay.aspx? ProductCategoryID={0}" /> <asp:BoundField HeaderText="Name" DataField="Name" /> <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> |
示例2首先声明了名为categorySource的ObjectDataSource控件。在查看代码之前,需要理解ObjectDataSource控件的两个重要属性。TypeName属性用于设置该控件绑定到的类名称。SelectMethod属性用于设置类调用的方法名称。使用这些属性能够设置类名称以及绑定到ObjectDataSource控件的类方法。对于categorySource控件而言,这些属性分别设置为“AdventureWorksTraderBiz.ProductCategoryBiz”和“GetProductCategories”。下一步,将categorySource控件的数据绑定到名为gridCategories的GridView控件,方法是将GridView的DataSourceID属性值设置为categorySource.
注意,ObjectDataSource控件还通过设置缓存属性,例如EnableCaching,CachDuration和SqlCacheDependency来实现缓存功能。将SqlCacheDependency属性设置为CommandNotification,指示ASP.NET应该为ObjectDataSource控件创建基于通知的依赖。
另外,为了使用基于通知的依赖,需要在首次执行SQL查询之前,在应用程序中调用System.Data.SqlClient.SqlDependency.Start()方法。该方法可置于Global.asax文件的Application_Start()事件中。
SQL Server 2005的缓存依赖在接收更改通知的类型方面更具有灵活性。SQL Server 2005监视特定SQL命令结果集导致的修改。如果数据库中命令的结果集导致了变化,那么依赖功能会使缓存项无效。SQL Server 2005提供了行级别的通知。
| <%@ Page Language="" 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.当首次请求页面时,将使用默认值。
4. 产品显示过程
示例4列举列举了产品页的实现代码。
示例4:实现产品显示页面
| <%@ Page Language="C#" MasterPageFile="~/Common.master" Title="Products Display" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <asp:ObjectDataSource ID="productSource" runat="server" TypeName="AdventureWorksTraderBiz.ProductBiz" SelectMethod="GetProducts"> <SelectParameters> <asp:QueryStringParameter QueryStringField="ProductSubcategoryID" Direction="Input" Name="productSubcategoryID" DefaultValue="1" Type="Int32" /> </SelectParameters> </asp:ObjectDataSource> <asp:Label runat="server" ID="lblHeading" Font-Size="Medium" Font-Underline="False" ForeColor="#0000C0"> List of Products </asp:Label><br /> <br /> <asp:GridView HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White" AutoGenerateColumns="False" ID="gridProducts" runat="server" DataSourceID="productSource"> <Columns> <asp:BoundField ReadOnly="True" HeaderText="ProductID" DataField="ProductID" /> <asp:BoundField HeaderText="Name" DataField="Name" /> <asp:BoundField HeaderText="Product Number" DataField="ProductNumber" /> <asp:BoundField HeaderText="Color" DataField="Color" /> <asp:BoundField HeaderText="ListPrice" DataField="ListPrice" /> <asp:BoundField HeaderText="Modified Date" HtmlEncode="false" DataFormatString="{0:MM/dd/yyyy}" DataField="ModifiedDate" /> </Columns> </asp:GridView> </asp:Content> |
文档说明:
相关文档
返回首页 | 关于本站 | | 友情链接 | 广告服务 | 意见建议 | 访客留言 | 本站论坛
Copyright© 2001-2006 ProgramBBS.com All Rights Reserved 版权所有©编程论坛
Email: 吉ICP备05009985号
感谢长春订餐网友情支持