母版页动态切换

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

    if (Session["masterPageFile"] != null)
        this.MasterPageFile = Session["masterPageFile"].ToString();
}
 

你可能感兴趣的:(session,object,File,null)