C# 如何更改我的页面';运行时的母版页?

C# 如何更改我的页面';运行时的母版页?,c#,asp.net,webforms,master-pages,C#,Asp.net,Webforms,Master Pages,在我的一个项目中有一个需求,我需要在运行时更改母版页 我的意思是,我需要应用检查,并在检查的基础上,特定的母版页可以调用到我的本地aspx页面 请帮我解决同样的问题 提前感谢:)例如: void Page_PreInit(Object sender, EventArgs e) { this.MasterPageFile = "~/NewMaster.master"; } 根据需要应用您的条件。来自。是。仅在PreInit page事件期间(即运行时开始处理请求之前)设置MasterPa

在我的一个项目中有一个需求,我需要在运行时更改母版页

我的意思是,我需要应用检查,并在检查的基础上,特定的母版页可以调用到我的本地aspx页面

请帮我解决同样的问题

提前感谢:)

例如:

void Page_PreInit(Object sender, EventArgs e)
{
    this.MasterPageFile = "~/NewMaster.master";
}

根据需要应用您的条件。来自。

是。仅在PreInit page事件期间(即运行时开始处理请求之前)设置MasterPageFile属性(因为使用母版页呈现页面发生在Init事件之前)

如果尝试在Init或Load事件处理程序中设置MasterPageFile属性,则会引发异常

是的,这是可能的,实现如下


要实现这一点,我们需要在页面呈现之前在Page_PreInit中编写代码

将以下代码放入您的代码隐藏:

if (Session["userType"] == "Admin") //check the user type
    this.Page.MasterPageFile = "~/Admin.master";
 else
    this.Page.MasterPageFile = "~/User.master";
希望这有帮助

if (Session["userType"] == "Admin") //check the user type
    this.Page.MasterPageFile = "~/Admin.master";
 else
    this.Page.MasterPageFile = "~/User.master";