65.9K
CodeProject is changing. Read more.
Home

Changing Master Page at Runtime

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.44/5 (17 votes)

Sep 5, 2006

CPOL
viewsIcon

107242

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 


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;