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?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Mar 23, 2012, 12:34 AM
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