5.3动态加载母版页

动态加载母版页
应用场合:
1)通过加载不同的母版页可以让网站用户自定义外观
2)品牌联合 需要自己的网站与合作伙伴的网站外观相同,以使用户从伙伴公司的网站转到自己的网站时,不会感到被链接到了不同的网站,则可通过从伙伴公司的网站传递过来的查询字符串来动态加载不同的母版页。

页面执行的生命周期中,首先进行的就是母版页和内容页的合并。
加载母版页的事件是PreInit

protected void Page_PreInit(object sender,EventArgs e)
{
if(Request["master"]!=null)
{
switch(Request["master"])
{
case "Dynamic1":
Profile.MasterPageFile="Dynamic1.master";
break;
case "Dynamic2":
Profile.MasterPageFile="Dynamic2.master";
break;
}
}
MasterPageFile=Profile.MasterPageFile;
}

Web.Config
<configuration>
<system.web>
<profile>
<add name="MasterPageFile" defaultValue="Dynamic1.master"/>
</profile>
</system.web>
</configuration>

为多个内容页动态加载母版页
public class DynamicMasterPage:Page
{
protected override void OnPreInit(EventArgs e)
{
this.MasterPageFile=(String)Context.Profile["MasterPageFile"];
base.OnPreInit(e);
}
}

在Web配置中注册:
<configuration>
<system.web>
<pages pageBaseType="DynamicMasterPage"/>
<profile>
<add name="MasterPageFile" defaultValue="Dynamic1.master"/>
</profile>
</system.web>
</configuration>

2011-4-28 11:19 danny
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值