Setting a Master Page Dynamically

To set an ASPNET master page dynamically, use the Page_PreInit function and set the Page.MasterPageFile property.  For example:

protected void Page_PreInit(object sender, EventArgs e)
  this.Page.MasterPageFile = "~/MasterPages/" + mymaster + ".Master";

In the above example, mymaster should be set by whatever your requirements dictate – read from DB/CMS/whatever.  The page (aspx) itself shouldn’t have any other reference to the master page (ie, no “MasterPageFile attribute in the page directive), however it will still need the content placeholders, of course.

Technorati Tags:

Print | posted on Tuesday, August 17, 2010 3:04 PM


No comments posted yet.

Your comment:


Copyright © Lance Robinson

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski