Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Changing Master Page at Runtime

0.00/5 (No votes)
5 Sep 2006 1  
Changing Master Page at Runtime by User Code.

Introduction


Some times we have to change MasterPage of a Page at runtime 
by user code below is the trick for changing master page at runtime 


There is a Event <CODE>Page_PreInit who will be executing just before 
the page render. 
We have to do code in this event like below

protected void Page_PreInit(object sender, EventArgs e) 
{ 
 if (Membership.GetUser() == null) //check the user weather user is logged in or not

    this.Page.MasterPageFile = "~/General.master";
 else
    this.Page.MasterPageFile = "~/myMaster.master";
}
we can change the content of master page from inherited page too like below

<CODE>Master.FindControl("nameLbl").Visible = true;

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here