How to Set Master Page dynamically?

Amol Kolekar picture Amol Kolekar · Jun 7, 2012 · Viewed 33.1k times · Source


I have requirement in which i want to set different Master Pages for the Same Page depending upon userid(i.e. for one user it must set one master page and for another user it must set another master ).Can we set different Master pages for any page dynamically?Please help...

Answer

Kapil Khandelwal picture Kapil Khandelwal · Jun 7, 2012
void Page_PreInit(Object sender, EventArgs e)
{
    this.MasterPageFile = "~/MyMaster.master";
}

Explanation: You can attach a master page dynamically to a content page. Because the master page and content page are merged during the initialization stage of page processing, a master page must be assigned before then. Typically, you assign a master page dynamically during the PreInit stage.