通过设置“MasterPageFile”属性可以做到,然而这个属性只能在“Page_PreInit”事件之中或之前设置。
在Page_PreInit事件或之前,当前页面包含的对象还没有被生成,不能访问,所以,如果想根据当前页面上某个控件的值动态切换母板页是做不到的,能够做到的就是根据Session,或者QueryString等在页面打开之前已经赋值的变量来动态切换:
1
protected void Page_PreInit(object sender, EventArgs e)
2
{
3
if(Request.QueryString["masterPageFile"] != null)
4
this.MasterPageFile = Request.QueryString["masterPageFile"];
5
6
7
if(Session["masterPageFile"]!=null)
8
this.MasterPageFile = Session["masterPageFile"].ToString();
9
10
}

2



3

4

5

6

7

8

9

10
