Change the master page from code behind

ANP picture ANP · Aug 4, 2010 · Viewed 17.1k times · Source

I have a web page named MyPage.aspx and two master page named Home.master and BlanK.master. By default MyPage.aspx uses Home.master.But based on some condition I need to change the master page from Home.aspx to Blank.master.So, how to do this from code behind in c#?I mean how change the master page from code behind?

Answer

stuartd picture stuartd · Aug 4, 2010

Set it in the Pre_Init event:

void Page_PreInit(object sender, EventArgs e)
{
    MasterPageFile = "~/Blank.master";
}

See http://odetocode.com/Articles/450.aspx for some detail and more options.