<p>I'm working on a site which needs to be able to support two or more looks, changable at runtime. I'd hoped to be able to handle the change with a CSS switch, but it looks like I'll need to use a different masterpage for each design.</p>

<p>So, what's the best way to set the masterpage at runtime? Page.MasterPageFile can only be set in the Page.OnPreInit event. It looks like the solutions are to make all my pages inherit from a common base which handles the PreInit event, or to use an HttpModule which does that.</p>

<p>Any advice?</p>

[{'answer_id': 15235, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': '<p>Rather than two different master pages how about having one master that dynamically loads different user controls and content HTML literals?</p>\n'}, {'answer_id': 15239, 'author': 'thelsdj', 'author_id': 163, 'author_profile': 'https://Stackoverflow.com/users/163', 'pm_score': 4, 'selected': True, 'text': "<p>I've done this once before, I did exactly what you described (Made all pages inherit from a custom page with an OnPreInit event). Also I had a custom Application_PreRequestHandlerExecute in my Global.asax.cs for setting Page.StyleSheetTheme for doing image/css changes that didn't require a different Master Page.</p>\n"}, {'answer_id': 15242, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>It\'s easy enough to handle PreInit and insert the one line of code it takes to load the proper Master Page.</p>\n\n<pre><code>this.Page.MasterPageFile = "~/default.master";\n</code></pre>\n\n<p>In the absence of some compelling reason not to go this route, that\'s what I\'d do, regardless of where you handle the PreInit.</p>\n'}, {'answer_id': 15274, 'author': 'Brian G Swanson', 'author_id': 1795, 'author_profile': 'https://Stackoverflow.com/users/1795', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m curious what decides how the page should look? Is it the user clicking a button to change the theme? Is it based on the URL that was used to get to the site?</p>\n\n<p>Code behind is supported in Master Pages, so you could put some logic in your one Master Page to decide what should be displayed.</p>\n\n<p>I\'ve seen several sites set cookies based on user clicks (to change font size, or page width), and then have different CSS files applied based on the value of those cookies.  If no cookie is present, display a default look and feel.</p>\n\n<p>EDIT:</p>\n\n<p>Another thought here, if you are simply trying to switch out CSS is to set your style tag to run at the server, and assign properties to it at run-time.  Once again this would require the use of a single master page, and putting code the code-behind of the master page, probably in the PreInit event handler.</p>\n\n<p>Since I\'ve never implemented this solution I\'m not sure if the whole &lt;HEAD&gt; tag has to run at the server or not.</p>\n\n<pre><code>\n&lt;html&gt;\n&lt;head id="Head" runat="server"&gt;\n&lt;style id="StylePlaceholder" runat="server" type="text/css"&gt;&lt;/style&gt;\n&lt;/head&gt;\n</code></pre>\n'}, {'answer_id': 578509, 'author': 'Paul', 'author_id': 44636, 'author_profile': 'https://Stackoverflow.com/users/44636', 'pm_score': 2, 'selected': False, 'text': '<p>I feel your pain. I searched for about an hour (if not more) for an issue to this, without success. It isn\'t just a cut and dry answer to say "just call it from PreInit on each page" when you have hundreds of pages. But then I realized that I was spending more time looking for a solution than it would have taken to just do it on each page.</p>\n\n<p>However, I wanted to set the MasterPageFile based on a Profile property, so that would have been about 5 lines of code each page, a maintainability nightmare. And anyways, "don\'t repeat yourself", right?</p>\n\n<p>So I created an Extension method in a module in the App_Code folder to make this easier and more maintainable.</p>\n\n<pre><code>Public Module WebFunctions\n\n    &lt;System.Runtime.CompilerServices.Extension()&gt; _\n    Public Sub SetMaster(ByVal page As Page)\n\n        Dim pb As ProfileCommon = DirectCast(HttpContext.Current.Profile, ProfileCommon)\n\n        If pb IsNot Nothing Then\n            page.MasterPageFile = pb.MasterPage\n        End If\n\n    End Sub\n\nEnd Module\n</code></pre>\n\n<p>And then on each page\'s PreInit, I just call this:</p>\n\n<pre><code>Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit\n        Me.SetMaster()\n    End Sub\n</code></pre>\n'}, {'answer_id': 18512238, 'author': 'H.M.', 'author_id': 1218928, 'author_profile': 'https://Stackoverflow.com/users/1218928', 'pm_score': 0, 'selected': False, 'text': '<p>Inherit all you pages from a base class like</p>\n\n<pre><code>public class PageBase : System.Web.UI.Page\n{\n    public PageBase()\n    {\n        this.PreInit += new EventHandler(PageBase_PreInit);\n    }\n    void PageBase_PreInit(object sender, EventArgs e)\n    {\n        this.MasterPageFile = "~/MyMasterPage.Master";\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/15228', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5/']