42537

ASP.Net Dynamically switch Master Pages

Question:

Never needed to do this before but is it possible to dynamically set/change which master page a page is using? Have an old asp.net web forms project which I have created a new bootstrap template for but the boss wants to give people the opportunity to switch on the new one instead of forcing it upon them.

Answer1:

I would recommend you to create a BasePage class than write this method in that class and inherit all of your pages from this class whose master page can be changed dynamically.

public class BasePage: System.Web.UI.Page { protected void Page_PreInit(object sender, EventArgs e) { try { if (conduction1) this.Page.MasterPageFile = "~/MasterPage.master"; else this.Page.MasterPageFile = "~/Master.master"; } catch (Exception ex) { } } }

And then in your page inherit page from BasePage like this

public partial class _Default:BasePage

Answer2:

The master page is changed only in preint event

protected void Page_PreInit(object sender, EventArgs e) { try { if (conduction1) this.Page.MasterPageFile = "~/MasterPage.master"; else this.Page.MasterPageFile = "~/Master.master"; } catch (Exception ex) { } }

or

void page_PreInit(object sender, EventArgs e) { Page page = sender as Page; page.MasterPageFile = "string location of masterpage"; }

Recommend

  • post json to java server
  • How does Booking.com close their app programmatically?
  • Force VSProps settings to override project settings
  • mysql how to find the total number of child rows with respect to a parent
  • Is it better to use local variables or chain methods inline? [closed]
  • How invoke APEX method from custom button
  • How to properly trigger lazy load of collection in Entity Framework 5?
  • What does VC/C++ naked attribute do?
  • Dropdown menu with the dropdown-menu-right class does not align to the right
  • Looking for datastructure that maintains a size & purges older elements in the process
  • Avoiding try/catch hell in my web pages
  • How do I control the soft menu button in Honeycomb?
  • Git subtree post hook
  • Grails Transactions and the Session
  • Ant: fileset “dir” attribute with a runtime expanded full path
  • Fixed Background Works in Chrome but Not Firefox?
  • How to best manage SMTP clients
  • Swift iOS don't recognize GCM library imports?
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • WordPress > setting permalink option via script buggy?
  • Hide HTML elements without javascript, only CSS
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • JqueryMobile Popup menu is not working
  • Copy to all folders batch file?
  • Declaring variable dynamically in VB.net
  • Installing iPhone App to iPhone
  • Abort upload large uploads after reading headers
  • Moving mysql files across servers
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Linq Objects Group By & Sum
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • Optimizing database types to compact database (SQLite)
  • Trying to switch camera back to front but getting exception
  • Data Validation Drop Down Box Arrow Disappearing
  • Buffer size for converting unsigned long to string
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Programmatically clearing map cache
  • reshape alternating columns in less time and using less memory