Click here to Skip to main content
Licence CPOL
First Posted 5 Sep 2006
Views 42,355
Bookmarked 20 times

Changing Master Page at Runtime

By | 5 Sep 2006 | Article
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

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)

About the Author

mehmoodmi

Software Developer (Senior)

Pakistan Pakistan

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmemberstuti IT23:20 2 May '12  
GeneralMy vote of 3 Pinmembervaibhav mahajan23:17 12 Apr '12  
QuestionWhat to write in HTML part of Content Page? Pinmembermiss_neha_mishra0:29 3 Aug '07  
What do I write in HTML part of Content Page?
 
<%@ Page Language="C#" MasterPageFile="?????" CodeFile="Page1.aspx.cs" Inherits="Page1" %>
 
Neha Mishra

QuestionThanks Alot Pinmemberbabamome22:49 9 Jul '07  
QuestionHost a website over free server as urs http://minhajkk.brinkster.net/ PinmemberMeEEsingYou1:57 26 Jun '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 5 Sep 2006
Article Copyright 2006 by mehmoodmi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid