Click here to Skip to main content
14,634,449 members
Articles » Web Development » ASP.NET » General
Posted 5 Sep 2006


21 bookmarked

Changing Master Page at Runtime

Rate this:
3.44 (17 votes)
Please Sign up or sign in to vote.
3.44 (17 votes)
5 Sep 2006CPOL
Changing Master Page at Runtime by User Code.


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

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";
    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>


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


About the Author

Software Developer (Senior)
Pakistan Pakistan
No Biography provided

Comments and Discussions

Questiontanqz a ton Pin
safeer78625-Sep-12 23:03
Membersafeer78625-Sep-12 23:03 
QuestionMaster Type? Pin
Member 863497225-Jun-12 11:37
MemberMember 863497225-Jun-12 11:37 
GeneralMy vote of 4 Pin
stuti IT2-May-12 23:20
Memberstuti IT2-May-12 23:20 
GeneralMy vote of 3 Pin
vaibhav mahajan12-Apr-12 23:17
Membervaibhav mahajan12-Apr-12 23:17 
QuestionWhat to write in HTML part of Content Page? Pin
miss_neha_mishra3-Aug-07 0:29
Membermiss_neha_mishra3-Aug-07 0:29 
QuestionThanks Alot Pin
babamome9-Jul-07 22:49
Memberbabamome9-Jul-07 22:49 
QuestionHost a website over free server as urs Pin
IMQ26-Jun-07 1:57
MemberIMQ26-Jun-07 1: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.