Logo 
Search:

Asp.net Answers

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds
  on Nov 17 In Asp.net Category.

  
Question Answered By: Adah Miller   on Nov 17

Yes you can load master page dynamically.

In order to do so you need to assign a master page inside Page_PreInit Event.

Master Page dynamic assignment can only be done inside Page_PreInit Event, since the rendering of the page with the master page occurs prior to the Init event. However, in the case where you want to load the master page dynamically, for example from a database entry based on a user's preference or something, you don't want to add code to the OnPreInit event on every page in the site. It is easy enough to add that code to a base page class that you can inherit from for you pages in the site.


public class MyPage : System.Web.UI.Page
{
protected override void Page_PreInit(Object sender, EventArgs e)
{
string masterfile = GetMasterPageFromDatabase();
if (!masterfile.Equals(string.Empty))
{
base.MasterPageFile = masterfile;
}
base.OnPreInit(e);
}
}

Share: 

 
 
Didn't find what you were looking for? Find more on How to dynamically assign Master Page Or get search suggestion and latest updates.


Tagged: