动态加载母版页

       在内容页中动态加载不同的母板页,这种方法适合在以上两个场合使用:
       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 

<?xml version="1.0"?>
<configuration>
  <system.web>
    <!--
    <pages pageBaseType="DynamicMasterPage" />
    -->

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


     

你可能感兴趣的:(动态)