通過設置“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
