asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET2.0:AdventureWorks贸易分析(2)


发布日期:2022年04月02日
 
ASP.NET2.0:AdventureWorks贸易分析(2)
本文将关注ASPNET Web站点的实现使用Visual Studio 可创建新的Web站点当Web站点创建后可添加指向业务逻辑组件AdventureWorksTraderBiz的引用注意与EntLib配置相关的所有配置设置都存储在nfig文件中本文的内容与业务过程密切相关主要包括产品类别显示过程产品子类别显示过程产品显示过程所涉及的Web站点在开始讲解这些过程之前首先说明用于整个Web站点的母版页

母版页

专业的Web站点中所有页面应该具有标准化的外观例如常用的布局之一是在页面左边布置有导航菜单版权信息在底部内容在中间如果在每个Web页面中复制通用的逻辑和外观来维护统一性是很困难的在ASPNET 使用母版页来实现这个工作就很简单了开发人员只需要在母版页中编写一次通用的内容即可母版页可作为一个或者多个Web页面的模板每个ASPX Web页面仅需要定义自身的唯一内容接着内容将插入母版页布局中的特定区域

示例显示了名为Commonmaster的母版页代码该母版页将用于AdventureWorks贸易站点

示例实现外观一致的母版页

<%@ Master Language=C# %>

<%@ Import Namespace=SystemIO %>

<%@ Import Namespace=SystemXml %>

<html xmlns=>

<head runat=server>

<title>Master Page</title>

</head>

<body>

<form id=form runat=server>

<div>

<asp:Table ID=tblTop BackColor=Gainsboro runat=server

Width=% Height=px ForeColor=DarkCyan>

<asp:TableRow runat=server HorizontalAlign=Center>

<asp:TableCell runat=server ColumnSpan=>

<asp:Label ID=Label runat=server ForeColor=Black

FontSize=Medium>AdventureWorks Trader System

</asp:Label>

</asp:TableCell>

</asp:TableRow>

</asp:Table>

<asp:Table ID=Table runat=Server Width=px>

<asp:TableRow runat=server>

<asp:TableCell runat=server>

<asp:Table ID=Table BackColor=Gainsboro runat=server

Width=% ForeColor=DarkCyan Height=px>

<asp:TableRow ID=TableRow runat=server

HorizontalAlign=Center>

<asp:TableCell ID=TableCell runat=server>

<asp:HyperLink runat=server Text=Product Categories

NavigateUrl=~/ProductCategoryDisplayaspx>

</asp:HyperLink>

</asp:TableCell>

</asp:TableRow>

<asp:TableRow ID=TableRow runat=server

HorizontalAlign=Center>

<asp:TableCell ID=TableCell runat=server>

<asp:HyperLink ID=HyperLink runat=server Text=Product

Sub Categories NavigateUrl=~/ProductSubcategoryDisplayaspx />

</asp:TableCell>

</asp:TableRow>

<asp:TableRow ID=TableRow runat=server HorizontalAlign=Center>

<asp:TableCell ID=TableCell runat=server>

<asp:HyperLink ID=HyperLink runat=server Text=Products NavigateUrl=~/ProductDisplayaspx />

</asp:TableCell>

</asp:TableRow>

</asp:Table>

</asp:TableCell>

<asp:TableCell runat=server>

<asp:Table ID=Table BackColor=Gainsboro runat=server Width=% ForeColor=DarkCyan

Height=px>

<asp:TableRow ID=TableRow runat=server>

<asp:TableCell BackColor=#FFFFE ID=TableCell runat=server>

<asp:contentplaceholder id=ContentPlaceHolder runat=server>

</asp:contentplaceholder>

</asp:TableCell>

</asp:TableRow>

</asp:Table>

</asp:TableCell>

</asp:TableRow>

</asp:Table>

</div>

</form>

</body>

</html>

母版页封装了Web站点所有页面的页头和左边导航信息既然读者已经了解了母版页下面讲解提供核心功能的内容页首先讨论产品类别显示过程

产品类别显示过程

在产品类别显示过程中用户可浏览AdventureWorks数据库中的类别列表另外用户还可以浏览属于一个产品类别的产品子类别通过单击产品类别名称用户能够访问产品子类别实例列举了页面的实现代码

示例实现继承自母版页的产品类别显示页面

<%@ Page Language=C# MasterPageFile=~/Commonmaster Title=Product Categories Display %>

<asp:Content ID=Content ContentPlaceHolderID=ContentPlaceHolder runat=Server>

<asp:ObjectDataSource ID=categorySource EnableCaching=true

SqlCacheDependency=CommandNotification CacheDuration=Infinite

TypeName=AdventureWorksTraderBizProductCategoryBiz

SelectMethod=GetProductCategories runat=server>

</asp:ObjectDataSource>

<asp:Label runat=server ID=lblHeading FontSize=Medium

FontUnderline=False ForeColor=#C>

Click on the Category to go to the SubCategories

</asp:Label><br />

<br />

<asp:GridView HeaderStyleHorizontalAlign=Center

HeaderStyleFontBold=True HeaderStyleBackColor=blue

HeaderStyleForeColor=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=ProductSubcategoryDisplayaspx? ProductCategoryID={} />

<asp:BoundField HeaderText=Name DataField=Name />

<asp:BoundField HeaderText=Row Guid DataField=Rowguid />

<asp:BoundField HeaderText=Modified Date HtmlEncode=false DataFormatString={:MM/dd/yyyy}

DataField=ModifiedDate />

</Columns>

</asp:GridView>

</asp:Content>

示例首先声明了名为categorySource的ObjectDataSource控件在查看代码之前需要理解ObjectDataSource控件的两个重要属性TypeName属性用于设置该控件绑定到的类名称SelectMethod属性用于设置类调用的方法名称使用这些属性能够设置类名称以及绑定到ObjectDataSource控件的类方法对于categorySource控件而言这些属性分别设置为AdventureWorksTraderBizProductCategoryBizGetProductCategories下一步将categorySource控件的数据绑定到名为gridCategories的GridView控件方法是将GridView的DataSourceID属性值设置为categorySource

注意ObjectDataSource控件还通过设置缓存属性例如EnableCachingCachDuration和SqlCacheDependency来实现缓存功能将SqlCacheDependency属性设置为CommandNotification指示ASPNET应该为ObjectDataSource控件创建基于通知的依赖

另外为了使用基于通知的依赖需要在首次执行SQL查询之前在应用程序中调用SystemDataSqlClientSqlDependencyStart()方法该方法可置于Globalasax文件的Application_Start()事件中

SQL Server 的缓存依赖在接收更改通知的类型方面更具有灵活性SQL Server 监视特定SQL命令结果集导致的修改如果数据库中命令的结果集导致了变化那么依赖功能会使缓存项无效SQL Server 提供了行级别的通知

产品子类别显示过程

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

示例产品子类别显示页面

<%@ Page Language=C# MasterPageFile=~/Commonmaster Title=Product Sub Category Display %>

<asp:Content ID=Content ContentPlaceHolderID=ContentPlaceHolder runat=Server>

<asp:ObjectDataSource ID=subCategorySource

TypeName=AdventureWorksTraderBizProductSubcategoryBiz

SelectMethod=GetProductSubCategories runat=server>

<SelectParameters>

<asp:QueryStringParameter QueryStringField=ProductCategoryID

Direction=Input Name=productCategoryID DefaultValue=

Type=Int />

</SelectParameters>

</asp:ObjectDataSource>

<asp:Label runat=server ID=lblHeading FontSize=Medium

FontUnderline=False ForeColor=#C>

Click on the SubCategory to go to the Products

</asp:Label><br />

<br />

<asp:GridView HeaderStyleHorizontalAlign=Center

HeaderStyleFontBold=True HeaderStyleBackColor=blue

HeaderStyleForeColor=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=ProductDisplayaspx? ProductSubcategoryID={} />

<asp:BoundField HeaderText=Row Guid DataField=Rowguid />

<asp:BoundField HeaderText=Modified Date HtmlEncode=false DataFormatString={:MM/dd/yyyy} DataField=ModifiedDate />

</Columns>

</asp:GridView>

</asp:Content>

示例包括名为subCategorySource的ObjectDataSource控件该控件绑定了ProductSubcategoryBiz类的GetProductSubCategories()方法正如前文讲解的那样GetProductSubCategories()方法可接受产品类别ID为参数同时返回属于该产品类别的所有子类别信息为了调用这个方法subCategorySource控件应该将产品类别ID(由产品类别显示页面返回)传递给该方法在这种情况下使用QueryStringParameter集合获取产品类别ID为此将QueryStringParameter模板的QueryStringField设置为查询字符串字段名称同时将Name属性设置为GetProductSubcategories()方法参数的名称这样在前面页面选中的产品类别ID则用于SQL查询的参数开发人员还可以使用DefaultValue属性设置产品类别ID默认值为当首次请求页面时将使用默认值

产品显示过程

示例列举列举了产品页的实现代码

示例实现产品显示页面

<%@ Page Language=C# MasterPageFile=~/Commonmaster Title=Products Display %>

<asp:Content ID=Content ContentPlaceHolderID=ContentPlaceHolder

runat=Server>

<asp:ObjectDataSource ID=productSource runat=server

TypeName=AdventureWorksTraderBizProductBiz

SelectMethod=GetProducts>

<SelectParameters>

<asp:QueryStringParameter QueryStringField=ProductSubcategoryID

Direction=Input Name=productSubcategoryID DefaultValue=

Type=Int />

</SelectParameters>

</asp:ObjectDataSource>

<asp:Label runat=server ID=lblHeading FontSize=Medium

FontUnderline=False

ForeColor=#C>

List of Products

</asp:Label><br />

<br />

<asp:GridView HeaderStyleHorizontalAlign=Center

HeaderStyleFontBold=True HeaderStyleBackColor=blue

HeaderStyleForeColor=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={:MM/dd/yyyy}

DataField=ModifiedDate />

</Columns>

</asp:GridView>

</asp:Content>

示例中所示的代码与示例非常类似除了本页面中的ObjectDataSource控件调用了不同的方法来获取所有产品

小结

在这个实例中读者已经学习了各种很好的用于创建高效ASPNET Web站点的实践技巧包括使用EntLib应用程序块以及ASPNET和SQL Server 的新功能该实例所讨论的应用程序说明了将应用程序块集成到真实ASPNET站点的方法同时还讲解了泛型集合的使用方法此外本实例还讨论了ObjectDataSource控件如何支持分层应用程序设计从而允许开发人员直接将对象方法的输出绑定到ASPNET页面中的控件希望读者在自己的项目中能够因地制宜的应用这些技术特性

上一篇:理解ASP.NET与客户端缓存之HTTP协议

下一篇:ASP.NET中利用HTTP报头状态码实现SEO