How to dynamically change the main page of the main page?

I am trying to change the main page dynamically, and although this is easy to do from the content page (overriding OnPreInit ), there is no such event for the main page. Is it possible to somehow represent this event?

UPDATE: I got halfway through going through the PreInit pages at the bottom of the stairs, it turns out you can do something like base.Master.MasterPageFile = "/master.Master"; , but for some reason it doesnโ€™t. Download the material in the title of the most important main page, namely the style sheet.

+4
source share
4 answers

Quote from: Can I change the dynamic master of a nested homepage?

Just test this, and it works from the PreInit page, which uses the embedded MasterPage. protected void Page_PreInit (object sender, EventArgs e)
{
this.Master.MasterPageFile = "/Site2.Master";
}

Obviously, you need to make sure that the ContentPlaceholderIds are consistent between the pages you are changing.

+3
source

If you redefined MasterPageClass and added your own onPreInit, you could do it, but I don't think even that will work. There is definitely no construct for it according to the Reflector, but it doesnโ€™t even override, since it inherits UserControl, then it is always OnInit ... alternately you can try to override get_Master (), but this may not work ...

+1
source

Use the homepage constructor.

0
source

Suppose you want to use a different main page without a menu, pass the NoMenu query string.

 protected void Page_PreInit(object sender, EventArgs e) { //You'll go through infinite loop if you do not check if we already have the new master page, this will switch to different master page if requested without a menu for example if (Request.QueryString["NoMenu"] != null && this.MasterPageFile != "/MasterPageNoMenu.master") { this.MasterPageFile = "/MasterPageNoMenu.master"; base.OnPreInit(e); } } 
0
source

All Articles