熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .NET編程 
本文將關注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頁面中的控件希望讀者在自己的項目中能夠因地制宜的應用這些技術特性
 


From:http://tw.wingwit.com/Article/program/net/201311/13298.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.