Click here to Skip to main content
12,695,365 members (28,281 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

Changing Master Page at Runtime

, 5 Sep 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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>

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

You may also be interested in...

Comments and Discussions

Questiontanqz a ton Pin
safeer78626-Sep-12 0:03
membersafeer78626-Sep-12 0:03 
QuestionMaster Type? Pin
Member 863497225-Jun-12 12:37
memberMember 863497225-Jun-12 12:37 
GeneralMy vote of 4 Pin
stuti IT3-May-12 0:20
memberstuti IT3-May-12 0:20 
GeneralMy vote of 3 Pin
vaibhav mahajan13-Apr-12 0:17
membervaibhav mahajan13-Apr-12 0:17 
QuestionWhat to write in HTML part of Content Page? Pin
miss_neha_mishra3-Aug-07 1:29
membermiss_neha_mishra3-Aug-07 1:29 
QuestionThanks Alot Pin
babamome9-Jul-07 23:49
memberbabamome9-Jul-07 23:49 
QuestionHost a website over free server as urs Pin
MeEEsingYou26-Jun-07 2:57
memberMeEEsingYou26-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 5 Sep 2006
Article Copyright 2006 by mehmoodmi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid