您当前的位置:中客资源站网络学院.NET专区综合应用 → 文章内容 退出登录 用户管理
本类热门文章
相关下载
ASP.NET2.0+SQL Server2005构建多层应用
作者:中客资源  来源:中客资源  发布时间:2007-2-7 2:49:33

减小字体 增大字体

entPlaceHolder控件,在该控件中,将展现其他子页面的内容。在其他子页面中,只需要首先引用该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控件的特性,可以很方便地沟通表示层和逻辑层


上一页  [1] [2] [3] 

[] [返回上一页] [打 印]
文章评论 (评论内容只代表网友观点,与本站立场无关!)

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码