您的位置:首页 > 其它

MasterPage的另外一种实现方式

2006-02-21 22:42 330 查看
我们知道WEB页面最终呈现给用户的是HTML,如下所示的一种格式:<Html><body><form><table><tr><td>(.....每个页面公共部分)....<table><tr></td>(..每个页面私有部分)</td></tr></table></td></tr></table></body></Html>
为此我们可以建立一个模板页面,在<%和%>中的内容我们规定是每个页面的私有部分,这里的<%没有特别意义,只不过一个标志而已,如下我们可以建立一个MasterPage.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>模版页面</title>
</head>
<body>
<link href="../CSS/trafficprice.css" type="text/css" rel="stylesheet">
<table cellSpacing="0" cellPadding="0" width="100%" border="0" ID="Table1">
<tr bgColor="#336699">
<td colSpan="2" height="60">
<h1 class="style1">公司标头</h1>
</td>
</tr>
<tr>
<td colSpan="2"><%#TopMenu%></td>
</tr>
<tr>
<td colSpan="2"><%#UserStatus%></td> <!--~/UserControl/WebUserHelp.ascx -->
</tr>
<tr vAlign="top">
<td bgColor="#dbeaf5"><%~/UserControl/menu.ascx%></td>
<td width="100%"><%CONTENT%></td>
</tr>
<tr bgColor="#336699">
<td colSpan="2" height="30"><span class="style1">联系人、版权信息</span></td>
</tr> </table>
</body>
</html>
〈%CONTENT%〉是表示其中的内容将被应用模板的页面内容所取代。对于ASP。NET的页面生成情况,我们可以把所有的元素归结为三种控件LiteralControl,编译是就能确认的用户控件(叫做WebCtrl)在模本中以<%~/UserControl/menu.ascx%> ~/UserControl/menu.ascx表示用户控件的路径,还有一种情况就是编译是系统不知道具体的用户控件在运行时才知道的(比如用户没有权限执行用户控件的功能,那就没有必要加载该用户控件),我们暂且就他为延迟用户控件DelayDefCtrl,在模板中用〈%TopMenu%〉表示。针对上面三种情况,我们定义三种控件类(LiteralItem、WebCtrlItem、DelayDefItem),和一个基类(MasterPageItem)
using System;
using System.Web.UI;

namespace TrafficPrice.CommunalAccess.TempletPage
using System;
using System.Web.UI;

namespace TrafficPrice.CommunalAccess.TempletPage

using System;
using System.Web.UI;

namespace TrafficPrice.CommunalAccess.TempletPage

using System;
using System.Web.UI;

namespace TrafficPrice.CommunalAccess.TempletPage
接下来我们就可以分析模板页面并生成上面所说的三种控件,下面是具体的实现类(MasterPageTemplate.cs)
using System;
using System.IO;
using System.Text;
using System.Web;
using System.Collections;
using System.Collections.Specialized;
namespace TrafficPrice.CommunalAccess.TempletPage
最后我们实现一个BasePage,它是一个继承自Page的一个类,我们让所有应用MasterPage的页面都继承这个类,通过重写CreateChildControls()方法,加载我们生成的三种控件。
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Security.Principal;
using System.Collections;
using System.Collections.Specialized;
//using TrafficPrice.CommunalAccess.DataDefine;

namespace TrafficPrice.CommunalAccess.TempletPage
下面是应用摸板的例子:
<%@ Register TagPrefix="uc1" TagName="pagenavigation" Src="../UserControl/pagenavigation.ascx" %>
<%@ Page language="c#" Codebehind="BrowBulkGoods.aspx.cs" AutoEventWireup="false" Inherits="TrafficPrice.SeaTransport.BrowBulkGoods" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>浏览散货信息</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:placeholder id="memMasterPagePrefix" runat="server"></asp:placeholder>
<table cellSpacing="0" cellPadding="0" width="95%" border="0">
<tr>
<td vAlign="top">
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td>this is a test for apply MasterPage
</td>
</tr>
</table>
</td>
</tr>
</table>
<asp:placeholder id="memMasterPageSuffix" runat="server"></asp:placeholder></form>
</body>
</HTML>
注意被在两个<asp:placeholder id="memMasterPagePrefix" runat="server">包围的内容将取代摸板页中〈%CONTENT%〉中的内容。如果有多个〈%CONTENT%〉那么也应该有多个<asp:placeholder 来分割。当然该类继承自BasePage,基类的构造函数的参数为摸板页面的路径。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: