Senator Guerra Souty original series calendar,replica hublot blue steel peach pointer collocation of rolex replica Rome digital scale, track type minute replica watches scale shows that the classical model is swiss replica watches incomparable, wearing elegant dress highlights.


YOU ARE HERE: HOME Questions Can we set Master page dynamically at runtime

Change the Master Pages Dynamically

View(s): 36735

Can we set Master page dynamically at runtime?

Answer 1)

. 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.

  <!-- ... -->
  <pages pageBaseType="MyWeb.UI.MyPageBase" />
  <!-- ... -->

  Asked in:  TCS (Tata Consultancy Services)   Expertise Level:  Intermediate
  Last updated on Saturday, 05 January 2013
5/5 stars (15 vote(s))

Register Login Ask Us Write to Us Help