2. 产品类别显示过程
在产品类别显示过程中,用户可浏览AdventureWorks数据库中的类别列表。另外,用户还可以浏览属于一个产品类别的产品子类别。通过单击产品类别名称,用户能够访问产品子类别。实例2列举了页面的实现代码。
示例2:实现继承自母版页的产品类别显示页面
<%@ Page Language="C#" MasterPageFile="~/Common.master" Title="Product Categories Display" %>示例2首先声明了名为categorySource的ObjectDataSource控件。在查看代码之前,需要理解ObjectDataSource控件的两个重要属性。TypeName属性用于设置该控件绑定到的类名称。SelectMethod属性用于设置类调用的方法名称。使用这些属性能够设置类名称以及绑定到ObjectDataSource控件的类方法。对于categorySource控件而言,这些属性分别设置为“AdventureWorksTraderBiz.ProductCategoryBiz”和“GetProductCategories”。下一步,将categorySource控件的数据绑定到名为gridCategories的GridView控件,方法是将GridView的DataSourceID属性值设置为categorySource。
<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>
注意,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提供了行级别的通知。