Hi friends,
I want to know that can we set Master Page dynamically at runtime? If it is possible then tell me how it will set?
Set Master page dynamically
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 22, 2012, 8:34 PM EDT
Yes. Set the MasterPageFile property only during the PreInit page event—that is, before the runtime begins working on the request (since the rendering of the page with the master
page occurs prior to the Init event)
protected void Page_PreInit(object sender, EventArgs e)
{
MasterPageFile = "simple2.master";
}
If you try to set the MasterPageFile property in Init or Load event handlers, an exception is raised.
Note: The Master property represents the current instance of the master page object, is a read-only property, and can't be set programmatically. The Master property is set by the
runtime after loading the content of the file referenced by the MasterPageFile property.
The above code needs to add in every page of the site. Instead, it is easy enough to add that code to a base page class that you can inherit from for you pages in the site.
In case if you do not already have the base page, you can use the following web.config settings to have a base class without having to modify the existing aspx pages.
Please refer the below link
http://mr-ponna.com/Question/413/Can-we-set-Master-page-dynamically-at-runtime-/
http://www.codeproject.com/Articles/15479/Changing-Master-Page-at-Runtime
Thanks