Put your code to change the master page in Page_PreInit
event.
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "MasterPage.master";
}
Question
I have an asp page employeeHome.aspx and I have two master pages adminMasterPage.master for Admin login and userMasterPage.master for normal user login as I have 2 types of user login one as Admin and other normal user. And I want to set adminMasterPage.master as a master page for employeeHome.aspx in case of normal user login (just for this page). How can I do this?
Solution
Put your code to change the master page in Page_PreInit
event.
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "MasterPage.master";
}
OTHER TIPS
Put the code at employeeHome.aspx
page
protected void Page_PreInit(object sender, EventArgs e)
{
if (Session["empCode"] != null)
{
if (Session["empCode"].ToString() != "0")
{
this.MasterPageFile = Server.MapPath("adminMasterPage.master");
}
}
}
You can change that by having the required master page file specified in the PreInit event, which is a part of the page life cycle..
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "adminMasterPage.master"; //For Admin
//this.MasterPageFile = "userMasterPage.master"; - For Normal User
}
This worked this way and here what I did as suggest by @Iswanto San and made changes in the path.
protected void Page_PreInit(object sender, EventArgs e)
{
if (Session["IsEmporAdm"].ToString() == "0")
{
this.MasterPageFile = "~/StyleLibrary\\layout\\AdminMaster.Master";
}
else
{
this.MasterPageFile = "~/StyleLibrary\\layout\\UserMaster.Master";
}
}