Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

How to dynamically assign Master Page

  Date: Nov 17    Category: Asp.net    Views: 724
  

I have a requirement where i want to display master page dynamically. My Company's website renders content in 4 format.

1 - No Columnar Master Page
2 - Two Columnar Master Page (Navigation on Left)
3 - Two Columnar Master Page (Navigation on Right)
4 - Three Columnar Master Page.

For one specific page i want to switch between master page dynamically. I have database field call "MasterPageFormat" which will return from 1 to 4, as described above.

Can someone help me in displaying master page based on value stored in database.

Share: 

 

1 Answer Found

 
Answer #1    Answered 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);
}
}

 
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: