跳过导航及工具栏
图书
出版社
作者
书评
在线阅读
资讯站
专题
高级搜索
订阅RSS
图书搜索:
卓越(
配送费全免
)
当当
蔚蓝书店
卓越网免运费,每单满100送20元,满200送40元
当当网免运费,再享95折上折,每单满100返10元A券
蔚蓝网专业图书我最全!
您的当前位置:
我要购书网
>
在线阅读
>
ASP/PHP/.NET/XML
asp.net2.0+sql server2005构建多层应用(4)
来源:互联网 作者: 发布:我要购物网收集整理 发布时间:2006-8-31 人气:274
创建逻辑层 接下来,我们创建逻辑层,在这个例子中,逻辑层是十分简单的,只是起到说明作用。首先,我们新建一个类AuthroBiz类,并将其放在App_code文件夹中,并将类的代码修改如下:public class AuthorsBiz{ public AuthorsBiz() {} public DataTable GetAuthors() { AuthorsTableAdapters.AuthorsTableAdapter authorDB = new AuthorsTableAdapters.AuthorsTableAdapter(); return authorDB.GetAuthors(); } public DataTable GetAuthorTitles(string authorID) { AuthorsTableAdapters.AuthorTitlesTableAdapter authorDB = new AuthorsTableAdapters.AuthorTitlesTableAdapter(); return authorDB.GetTitlesByAuthor(authorID); }} 从上面的代码中,可以看到,我们刚才通过向导创建的"Authors.xsd"类型化dataset类,现在在代码中,可以通过使用AuthorsTableAdapters类来调用,其中authorDB是AuthorsTableAdapters类的实例。 创建表示层 在ASP.NET 2.0中,在创建表示层时,可以使用master-page技术,使得可以很方便地构建页面。Mater-page的意思是,可以首先构建出一个页面的主框架模版结构,然后在其中放置一个ContentPlaceHolder控件,在该控件中,将展现其他子页面的内容。在其他子页面中,只需要首先引用该master页面,然后再修改ContentPlaceHolder控件的内容就可以了。 首先,在工程中新增加一个"master"类型的文件,将其命名为CommonMaster,然后输入以下代码:<%@ master language="C#" %><html> <head id="Head1" runat="server"> <title>Master Page</title> </head><body><form id="Form1" runat="server"> <table id="header" style="WIDTH: 100%; HEIGHT: 80px" cellspacing="1" cellpadding="1" border="1"> <tr> <td style="TEXT-ALIGN: center; width: 100%; height: 74px;" bgcolor="teal"> <asp:label runat="server" id="Header" Font-Size="12pt" Font-Bold="True"> Authors Information </asp:label> </td> </tr> </table> <b/> <table id="leftNav" style="WIDTH: 108px; HEIGHT: 100%" cellspacing="1" cellpadding="1" border="1"> <tr> <td style="WIDTH: 100px"> <table> <tr> <td> <a href=http://www.newbooks.com.cn/info/"Home.aspx">Home</a> </td> </tr> <tr> <td> <a href=http://www.newbooks.com.cn/info/"Authors.aspx">Authors List</a> </td> </tr> </table> </td> </tr> </table> <table id="mainBody" style="LEFT: 120px; VERTICAL-ALIGN: top; WIDTH: 848px; POSITION: absolute; TOP: 94px; HEIGHT: 100%" border="1"> <tr> <td width="100%" style="VERTICAL-ALIGN: top"> <asp:contentplaceholder id="middleContent" runat="Server"></asp:contentplaceholder> </td> </tr> </table></form></body></html> 接下来,我们首先创建以显示作者页面的Authors.aspx页面,由于页面的框架要保持一直,因此,可以利用maser-page技术,在新建页面时,引入刚才建立的CommonMaster页面,如下图:
点ADD按钮后,出现如下图,选择刚才建立的CommonMaster页面,如下图:
再输入如下代码:<%@ Page Language="C#" MasterPageFile="~/CommonMaster.master" %><asp:content id="Content1" contentplaceholderid="middleContent" runat="server"><asp:objectdatasource runat="server" id="authorsSource" typename="AuthorsBiz" selectmethod="GetAuthors"></asp:objectdatasource><asp:gridview runat="server" AutoGenerateColumns="false" id="authorsView" datasourceid="authorsSource"> <alternatingrowstyle 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="AuthorsBiz" selectmethod="GetAuthors"></asp:objectdatasource> 其中的typename属性指定为我们之前创建的逻辑层的类AuthorsBiz类,而为了获得数据,采用了selectmethod方法,这里指定了之前建立的GetAuthors方法。当然,也可以在其他场合,应用Updatemethod,Insertmethod,Deletemethod方法,也可以加上参数,比如接下来要创建的AuthorTitle.aspx页面,代码如下:<%@ Page Language="C#" MasterPageFile="~/CommonMaster.master" %><asp:content id="Content1" contentplaceholderid="middleContent" runat="server"><asp:objectdatasource runat="server" id="authorTitlesSource" typename="AuthorsBiz" selectmethod="GetAuthorTitles"><SelectParameters> <asp:QueryStringParameter Type="String" Direction="Input" Name="authorID" QueryStringField="AuthorID" /></SelectParameters></asp:objectdatasource><asp:gridview runat="server" id="authorTitlesView" datasourceid="authorTitlesSource"> <alternatingrowstyle backcolor="Silver"></alternatingrowstyle></asp:gridview></asp:content> 上面的代码中,首先用户在authors.aspx页面点选某个作者名时,则在authortitle.aspx页面中,返回该作者的所有著作。所以,在objectdatasource控件中,我们使用了SelectParameters参数,指定传入来要查询的参数是authorid。最后,再将gridview绑定到objectdatasource控件中去。 最后,运行我们的代码,结果如下两图所表示:
小结 在ASP.NET 2.0中,我们利用SQL Server 2005的强大功能,可以利用.NET 语言创建存储过程,并使用TableAdapter向导,很方便地创建数据访问层,再利用objectdatasource控件的特性,可以很方便地沟通表示层和逻辑层。
上一条:
vc++技术内幕(第四版)笔记(第7章)
下一条:
asp.net2.0+sql server2005构建多层应用(2)
相关文章
·
asc ii 完整码表及简介
·
asp.net中实现直接从网页上下载文件,而不须引用文件..
·
asp调用oracle存储过程
·
asp.net环境下,vb.net语言,数据保存exce..
·
asp.net中的mvc模式应用
·
asp.net2.0+sql server2005构建多..
·
asp实现的无限级会员树型结构
·
asp.net2.0+sql server2005构建多..
·
asp.net2.0+sql server2005构建多..
·
asp中巧用response属性
热点文章
·
creating user controls
·
提高fastreplace速度 (fstrrep.pas)
·
asc ii 完整码表及简介
·
自动生成拼音(汉字反查到拼音)
·
sql2000无法安装的解决办法
·
浏览器集成教学 自定义浏览器
·
vc++技术内幕(第四版)笔记(第7章)
·
mysql5.0中文乱码解决方案
·
sql server日期计算
·
vc下利用ado连接access数据库
%>
淘宝旺旺:我要购书网上书店『
图书目录
』
本购书中心地址: 杭州市延安路111号清波商厦南楼D座(总部) 上海市闸北区老沪太路网上购书中心(沪部), 邮编:310002
电子邮件:books@51goushu.com
经营许可证编号:沪ICP备06038574号
版权所有 2003-2008 © All Rights Reserved .
购书网