Click here to Skip to main content
15,851,387 members
Articles / Web Development / ASP.NET
Article

Changing Master Page at Runtime

Rate me:
Please Sign up or sign in to vote.
3.44/5 (17 votes)
5 Sep 2006CPOL 106.2K   21   7
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 


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

C#
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;</CODE>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiontanqz a ton Pin
safeer78626-Sep-12 0:03
safeer78626-Sep-12 0:03 
QuestionMaster Type? Pin
Member 863497225-Jun-12 12:37
Member 863497225-Jun-12 12:37 
GeneralMy vote of 4 Pin
stuti IT3-May-12 0:20
stuti IT3-May-12 0:20 
GeneralMy vote of 3 Pin
vaibhav mahajan13-Apr-12 0:17
vaibhav mahajan13-Apr-12 0:17 
QuestionWhat to write in HTML part of Content Page? Pin
miss_neha_mishra3-Aug-07 1:29
miss_neha_mishra3-Aug-07 1:29 
QuestionThanks Alot Pin
babamome9-Jul-07 23:49
babamome9-Jul-07 23:49 
QuestionHost a website over free server as urs http://minhajkk.brinkster.net/ Pin
IMQ26-Jun-07 2:57
IMQ26-Jun-07 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.