ASP.NET2.0+SQL Server2005構(gòu)建多層應(yīng)用
創(chuàng)建表示層
在ASP.NET 2.0中,在創(chuàng)建表示層時(shí),可以使用master-page技術(shù),使得可以很方便地構(gòu)建頁(yè)面。Mater-page的意思是,可以首先構(gòu)建出一個(gè)頁(yè)面的主框架模版結(jié)構(gòu),然后在其中放置一個(gè)ContentPlaceHolder控件,在該控件中,將展現(xiàn)其他子頁(yè)面的內(nèi)容。在其他子頁(yè)面中,只需要首先引用該master頁(yè)面,然后再修改ContentPlaceHolder控件的內(nèi)容就可以了。
首先,在工程中新增加一個(gè)"master"類型的文件,將其命名為CommonMaster,然后輸入以下代碼:
<%@ master language="C#" %>
<html>
?。糷ead id="Head1" runat="server">
?。紅itle>Master Page</title>
</head>
<body>
<form id="Form1" runat="server">
?。紅able id="header" style="WIDTH: 100%; HEIGHT: 80px" cellspacing="1" cellpadding="1" border="1">
<tr>
?。紅d style="TEXT-ALIGN: center; width: 100%; height: 74px;" bgcolor="teal">
?。糰sp:label runat="server" id="Header" Font-Size="12pt" Font-Bold="True">
Authors Information
</asp:label>
</td>
?。?tr>
</table>
?。糱/>
?。紅able id="leftNav" style="WIDTH: 108px; HEIGHT: 100%" cellspacing="1" cellpadding="1" border="1">
<tr>
?。紅d style="WIDTH: 100px">
?。紅able>
?。紅r>
?。紅d>
?。糰 href="Home.aspx">Home</a>
</td>
?。?tr>
<tr>
?。紅d>
<a href="Authors.aspx">Authors List</a>
?。?td>
</tr>
?。?table>
</td>
?。?tr>
?。?table>
?。紅able id="mainBody" style="LEFT: 120px; VERTICAL-ALIGN: top; WIDTH: 848px; POSITION: absolute; TOP: 94px; HEIGHT: 100%" border="1">
?。紅r>
?。紅d width="100%" style="VERTICAL-ALIGN: top">
?。糰sp:contentplaceholder id="middleContent" runat="Server"></asp:contentplaceholder>
?。?td>
</tr>
?。?table>
</form>
</body>
</html>
接下來(lái),我們首先創(chuàng)建以顯示作者頁(yè)面的Authors.aspx頁(yè)面,由于頁(yè)面的框架要保持一直,因此,可以利用maser-page技術(shù),在新建頁(yè)面時(shí),引入剛才建立的CommonMaster頁(yè)面,如下圖:

點(diǎn)ADD按鈕后,出現(xiàn)如下圖,選擇剛才建立的CommonMaster頁(yè)面,如下圖:

再輸入如下代碼:
<%@ Page Language="C#" MasterPageFile="~/CommonMaster.master" %>
<asp:content id="Content1" contentplaceholderid="middleContent" runat="server">
<asp:objectdatasource runat="server" id="authorsSource" typename="Author*iz" selectmethod="GetAuthors">
</asp:objectdatasource>
<asp:gridview runat="server" AutoGenerateColumns="false" id="authorsView" datasourceid="authorsSource">
?。糰lternatingrowstyle backcolor="Silver"></alternatingrowstyle>
<Columns>
<asp:HyperLinkField DataTextField="au_id" HeaderText="Author ID" DataNavigateUrlFields="au_id"
DataNavigateUrlFormatString="AuthorTitles.aspx?AuthorID={0}">
</asp:HyperLinkField>
<asp:BoundField HeaderText="Last Name" DataField="au_lname"></asp:BoundField>
<asp:BoundField HeaderText="First Name" DataField="au_fname"></asp:BoundField>
<asp:BoundField HeaderText="Phone" DataField="phone"></asp:BoundField>
<asp:BoundField HeaderText="Address" DataField="address"></asp:BoundField>
<asp:BoundField HeaderText="City" DataField="city"></asp:BoundField>
<asp:BoundField HeaderText="State" DataField="state"></asp:BoundField>
<asp:BoundField HeaderText="Zip" DataField="zip"></asp:BoundField>
</Columns>
</asp:gridview>
</asp:content>
注意,其中我們用到了objectdatasource控件,在.NET 2.0中,有了該控件,可以很方便地溝通表示層和邏輯層。其中的代碼如下:
<asp:objectdatasource runat="server" id="authorsSource" typename="Author*iz" selectmethod="GetAuthors">
</asp:objectdatasource>
其中的typename屬性指定為我們之前創(chuàng)建的邏輯層的類Author*iz類,而為了獲得數(shù)據(jù),采用了selectmethod方法,這里指定了之前建立的GetAuthors方法。當(dāng)然,也可以在其他場(chǎng)合,應(yīng)用Updatemethod,Insertmethod,Deletemethod方法,也可以加上參數(shù),比如接下來(lái)要?jiǎng)?chuàng)建的AuthorTitle.aspx頁(yè)面,代碼如下:
<%@ Page Language="C#" MasterPageFile="~/CommonMaster.master" %>
<asp:content id="Content1" contentplaceholderid="middleContent" runat="server">
<asp:objectdatasource runat="server" id="authorTitlesSource" typename="Author*iz" selectmethod="GetAuthorTitles">
<SelectParameters>
?。糰sp:QueryStringParameter Type="String" Direction="Input" Name="authorID" QueryStringField="AuthorID" />
</SelectParameters>
</asp:objectdatasource>
<asp:gridview runat="server" id="authorTitlesView" datasourceid="authorTitlesSource">
?。糰lternatingrowstyle backcolor="Silver"></alternatingrowstyle>
</asp:gridview>
</asp:content>
上面的代碼中,首先用戶在authors.aspx頁(yè)面點(diǎn)選某個(gè)作者名時(shí),則在authortitle.aspx頁(yè)面中,返回該作者的所有著作。所以,在objectdatasource控件中,我們使用了SelectParameters參數(shù),指定傳入來(lái)要查詢的參數(shù)是authorid。最后,再將gridview綁定到objectdatasource控件中去。
最后,運(yùn)行我們的代碼,結(jié)果如下兩圖所表示:


小結(jié)
在ASP.NET 2.0中,我們利用SQL Server 2005的強(qiáng)大功能,可以利用.NET 語(yǔ)言創(chuàng)建存儲(chǔ)過(guò)程,并使用TableAdapter向?qū)?,很方便地?chuàng)建數(shù)據(jù)訪問(wèn)層,再利用objectdatasource控件的特性,可以很方便地溝通表示層和邏輯層。 [/sell]
- 使用基于Node.js的構(gòu)建工具Grunt來(lái)發(fā)布ASP.NET MVC項(xiàng)目
- ASP.NET MVC3手把手教你構(gòu)建Web
- ASP.NET性能優(yōu)化之構(gòu)建自定義文件緩存
- Asp.net TreeView來(lái)構(gòu)建用戶選擇輸入的方法 推薦
- ASP.NET MVC+EF框架+EasyUI實(shí)現(xiàn)權(quán)限管系列
- ASP.NET中的Inherits、CodeFile、CodeBehind的區(qū)別詳解
- asp.net(c#)ref,out ,params的區(qū)別
- asp.net TemplateField模板中的Bind方法和Eval方法
- ASP.NET Ref和Out關(guān)鍵字區(qū)別分析
- ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后臺(tái)管理系統(tǒng)之前端頁(yè)面框架構(gòu)建源碼分享