Microsoft . 技术之路...

—— 专注于微软技术, 分享是快乐的源泉......
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ASP.NET2.0+SQL Server2005构建多层应用(4)

Posted on 2005-12-30 12:49  赣江源  阅读(136)  评论(0编辑  收藏  举报
创建逻辑层

  接下来,我们创建逻辑层,在这个例子中,逻辑层是十分简单的,只是起到说明作用。首先,我们新建一个类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="Home.aspx">Home</a>
     </td>
    </tr>
    <tr>
     <td>
      <a href="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控件的特性,可以很方便地沟通表示层和逻辑层。