Change the master page from code behind
Asked Answered
H

2

13

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?

Hennessey answered 4/8, 2010 at 11:35 Comment(0)
N
15

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.

Nina answered 4/8, 2010 at 11:39 Comment(0)
T
8

put the following line in the Page_PreInit method of your codebehind page:

protected void Page_PreInit(object sender, EventArgs e) 
{ 
    this.Page.MasterPageFile = "~/Blank.master";
}
Typewritten answered 4/8, 2010 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.