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.
<system.web>
<!-- ... -->
<pages pageBaseType="MyWeb.UI.MyPageBase"
/>
<!-- ... -->
</system.web>