Click here to Skip to main content
6,304,948 members and growing! (18,857 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Changing Master Page at Runtime

By Minhajkk

Changing Master Page at Runtime by User Code.
C# 2.0.NET 2.0, Win2K, WinXP, Win2003, Vista, ASP.NET, VS2005, Dev
Posted:5 Sep 2006
Views:15,007
Bookmarked:11 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
6 votes for this article.
Popularity: 1.88 Rating: 2.41 out of 5
3 votes, 50.0%
1

2
1 vote, 16.7%
3
1 vote, 16.7%
4
1 vote, 16.7%
5

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

About the Author

Minhajkk


Member
Minhajkk
i luv khipro
Occupation: Software Developer (Senior)
Location: Pakistan Pakistan

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralWhat to write in HTML part of Content Page? Pinmembermiss_neha_mishra1:29 3 Aug '07  
QuestionThanks Alot Pinmemberbabamome23:49 9 Jul '07  
QuestionHost a website over free server as urs http://minhajkk.brinkster.net/ PinmemberMeEEsingYou2:57 26 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Sep 2006
Editor:
Copyright 2006 by Minhajkk
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project