我们可以写一个基类,叫basepage.cs,放在app_code目录下,在这个类中,添加一个叫
runtimemasterpagefile的属性,是一个字符串类型,指定在运行期间才用哪一个模版文件,并且重写onpreinit
方法,代码如下:
public class basepage : system.web.ui.page
{
private string runtimemasterpagefile;
public string runtimemasterpagefile
{
get
{
return runtimemasterpagefile;
}
set
{
runtimemasterpagefile = value;
}
}
protected override void onpreinit(eventargs e)
{
if (runtimemasterpagefile != null)
{
this.masterpagefile = runtimemasterpagefile;
}
base.onpreinit(e);
}
}
接着,我们构造一个叫mainmaster.master的模版页,里面随便搞一个header和footer的信息,中间留一个
叫maincontent的contentplaceholder,然后再建一个叫submaster.master的模版页,其中的
masterpagefile="~/mainmaster.master",以套用mainmaster模版页,其中放一个一行两列的表格,如下:
<asp:content id="foo" contentplaceholderid="maincontent" runat="server">
<table>
<tr>
<td width="300">
left column in submaster
<br />
<asp:contentplaceholder id="leftcolumn" runat="server">
</asp:contentplaceholder>
</td>
<td>
right column in submaster
<br />
<asp:contentplaceholder id="rightcolumn" runat="server">
</asp:contentplaceholder>
</td>
</tr>
</table>
</asp:content>
最后,在一个aspx页面中,这样指定
<%@ page language="c#" masterpagefile="" runtimemasterpagefile="submaster.master" codefilebaseclass="basepage" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" title="untitled page" %>
可以看到,在这里,我们不设置masyterpage的属性,而是指定了runtimemasterpagefile的属性为
submaster.master,这个是在运行时候才加载的模版,而codefilebaseclass属性指定了我们刚才写
的那个类basepage.cs,这样,我们就可以在这个aspx的设计视图状态下看到拉,可以拖拉设计了。
要注意的是,根据微软的说法,听说要到下一个版本的visual studio,才能完全支持模版嵌套时的完全设计视图
状态的切换哦
母版页可以嵌套,让一个母版页引用另外的页作为其母版页。利用嵌套的母版页可以创建组件化的母版页。例如,大型网站可能包含一个用于定义站点外观的总体母版页。然后,不同的网站内容合作伙伴又可以定义各自的子母版页,这些子母版页引用网站母版页,并相应定义合作伙伴的内容的外观。
与任何母版页一样,子母版页也包含文件扩展名 .master。子母版页通常会包含一些内容控件,这些控件将映射到父母版页上的内容占位符。就这方面而言,子母版页的布局方式与所有内容页类似。但是,子母版页还有自已的内容占位符,可用于显示其子页提供的内容。下面的三个页清单演示了一个简单的嵌套母版页配置。
以下为父母版页文件:
<% @ Master Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html > <body> <form id="Form1" runat="server"> <div> <h1>Parent Master</h1> <p> <font color="red">This is parent master content.</font> </P> <asp:ContentPlaceHolder ID="MainContent" runat="server" /> </div> </form> </body> </html>
<% @ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html > <body> <form id="Form1" runat="server"> <div> <h1>Parent Master</h1> <p> <font color="red">This is parent master content.</font> </P> <asp:ContentPlaceHolder ID="MainContent" runat="server" /> </div> </form> </body> </html>
以下为子母版页文件:
<%@ Master Language="VB" master="Parent.master"%> <asp:Content id="Content1" ContentPlaceholderID="MainContent" runat="server"> <asp:panel runat="server" id="panelMain" backcolor="lightyellow"> <h2>Child master</h2> <asp:panel runat="server" id="panel1" backcolor="lightblue"> <p>This is childmaster content.</p> <asp:ContentPlaceHolder ID="Content1" runat="server" /> </asp:panel> <asp:panel runat="server" id="panel2" backcolor="pink"> <p>This is childmaster content.</p> <asp:ContentPlaceHolder ID="Content2" runat="server" /> </asp:panel> </asp:panel> </asp:Content>
<%@ Master Language="C#" master="Parent.master"%> <asp:Content id="Content1" ContentPlaceholderID="MainContent" runat="server"> <asp:panel runat="server" id="panelMain" backcolor="lightyellow"> <h2>Child master</h2> <asp:panel runat="server" id="panel1" backcolor="lightblue"> <p>This is child master content.</p> <asp:ContentPlaceHolder ID="Content1" runat="server" /> </asp:panel> <asp:panel runat="server" id="panel2" backcolor="pink"> <p>This is child master content.</p> <asp:ContentPlaceHolder ID="Content2" runat="server" /> </asp:panel> </asp:panel> </asp:Content>
以下为引用子母版页的子文件:
<%@ Page Language="VB" master="Child.Master"%> <asp:Content id="Content1" ContentPlaceholderID="Content1" runat="server"> <asp:Label runat="server" id="Label1" text="Child label1" font-bold="true" /> <br> </asp:Content> <asp:Content id="Content2" ContentPlaceholderID="Content2" runat=server> <asp:Label runat="server" id="Label2" text="Child label2" font-bold=true/> </asp:Content>
<%@ Page Language="C#" master="Child.Master"%> <asp:Content id="Content1" ContentPlaceholderID="Content1" runat="server"> <asp:Label runat="server" id="Label1" text="Child label1" font-bold="true" /> <br> </asp:Content> <asp:Content id="Content2" ContentPlaceholderID="Content2" runat=server> <asp:Label runat="server" id="Label2" text="Child label2" font-bold=true/> </asp:Content>