百度空间 | 百度首页 
               
 
查看文章
 
ASP.NET的模板实现(c#)
2008年03月29日 星期六 11:33
ASP.NET里,使用的是面向对象的开发模式,可以这么理解---每个页面都是一个从SystemWeb.UI.Page继承的Class,这个类给我们提供了一些诸如缓存,表示,应答,请求等服务(说是方法也行)。通过面向对象的方法是不是有比用include更好的解决方法呢?当然,答案是肯定的。

实现

记得有句名言叫“任何问题都可以通过加入一个中间层来实现”,举个很简单的例子,我们常常使用Façade这个模式降低系统的耦合度,而我们又为什么要使用设计模式呢?主要是用来减小耦合提高复用的。

从所有的ASPX页面都由SystemWeb.UI.Page继承而来这点上来看,我们只需在ASPXSystemWeb.UI.Page之间加入一层,写一个我们自己的Class就可以使问题变得简单化,而在.NET框架中,允许用户自定义HTML代码(这点可以参考Web User Controls),这样解决问题的雏形就出来了,见下图。

图中的“自定义类”就相当于我们加入的一个中间层,该自定类继承System.Web.UI.Page这个基类,下面给出自定类的代码:

PageBase.cs

     public class PageBase:System.Web.UI.Page

     {

         public string PageTitle=测试模板;

         protected override void Render(System.Web.UI.HtmlTextWriter writer)

         {

              writer.Write(@<html><head>

                   <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>

                   <title> + this.PageTitle + </title></head>);

              writer.Write(@<body>

                   <table border='0' width='680'>

                       <tr>

                            <td width='160' bgcolor='#006699' align='center'><font color='#FFFFFF'><b><a href='index.aspx'>首页</a></b></font></td>

                            <td colspan='2' width='520'>广告条</td>

                       </tr>

                       <tr>

                            <td width='160' valign='top'>

                                 <p>导航</p>

                                 <p><a href='newContact.aspx'>添加联系人</a></p>

                                 <p>查找联系人</p>

                            </td>

                            <td width='10'> </td>

                            <td width='510'>

              );

              base.Render(writer);

              writer.Write(@</td></tr><tr><td width='100%' colspan='3'>页脚 </td></tr></table></body></html>);

             

         }

   }

上面的PageBase.cs就是我们的自定类,这样,我们在其他ASPX页面中就可以直接继承PageBase这个类,而非System.Web.UI.Page,下面分别是index.aspxnewContact.aspx的代码(分别包含index.aspx.csnewContact.aspx.cs):

Index.aspx

<%@ Page language=c# Codebehind=index.aspx.cs AutoEventWireup=false Inherits=wab.index %>

<form id=index method=post runat=server>

       <asp:DataGrid id=contacts runat=server Width=492px Height=104px></asp:DataGrid>

</form>

index.aspx.cs(继承自定义类PageBase

       public class index : PageBase

       {

              protected System.Web.UI.WebControls.DataGrid contacts;

      

              private void Page_Load(object sender, System.EventArgs e)

              {

                     // 在此处放置用户代码以初始化页面

              }

              #region Web Form Designer generated code

              override protected void OnInit(EventArgs e)

              {

                     //

                     // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。

                     //

                     InitializeComponent();

                     base.OnInit(e);

              }

             

              /// <summary>

              /// 设计器支持所需的方法 - 不要使用代码编辑器修改

              /// 此方法的内容。

              /// </summary>

              private void InitializeComponent()

              {   

                     this.Load += new System.EventHandler(this.Page_Load);

              }

              #endregion

       }

newContact.aspx

<%@ Page language=c# Codebehind=newContact.aspx.cs AutoEventWireup=false Inherits=wab.newContact %>

<form id=newContact method=post runat=server>

       <P><FONT face=宋体>名子</FONT>

              <asp:TextBox id=TextBox1 runat=server></asp:TextBox></P>

       <P><FONT face=宋体>姓氏</FONT>

              <asp:TextBox id=TextBox2 runat=server></asp:TextBox></P>

       <P>

              <asp:Button id=Button1 runat=server Text=Button></asp:Button></P>

</form>

newContact.aspx.cs(继承自定类PageBase

       public class newContact : PageBase

       {

              protected System.Web.UI.WebControls.TextBox TextBox1;

              protected System.Web.UI.WebControls.Button Button1;

              protected System.Web.UI.WebControls.TextBox TextBox2;

      

              private void Page_Load(object sender, System.EventArgs e)

              {

              }

              #region Web Form Designer generated code

              override protected void OnInit(EventArgs e)

              {

                     //

                     // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。

                     //

                     InitializeComponent();

                     base.OnInit(e);

              }

             

              /// <summary>

              /// 设计器支持所需的方法 - 不要使用代码编辑器修改

              /// 此方法的内容。

              /// </summary>

              private void InitializeComponent()

              {   

                     this.Load += new System.EventHandler(this.Page_Load);

              }

              #endregion

       }

以上就是ASP.NET中模板基本的实现方法,不过在此先声明一点,这么做会让系统的性能下降一点点,但是这一点点并不影响实际项目,但我相信这一点点的系统性能换来的是日后维护的方便,相信很值得。


类别:.net学习 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu