相信大家都用过asp.net 2.0下的嵌套的masterpage功能,该功能很强大,可以让用户先制作出模版页后,然后很容易地套用到其他页面中去。而且模版页还可以嵌套的,但嵌套后的模版页,我们会发觉有一个问题,那就是当使用嵌套的模版页后,在设计视图状态下,是没办法对其进行可视化操作的,那么如何解决这个问题呢?我找了下老外的blog,找到了老外提供的一个方法,暂且可以绕个弯来解决该问题,下面讲解一下:
我们可以写一个基类,叫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模版页,其中放一个一行两列的表格,如下:
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器