{"id":22,"date":"2008-04-02T11:47:22","date_gmt":"2008-04-02T15:47:22","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=22"},"modified":"2011-07-11T15:59:21","modified_gmt":"2011-07-11T20:59:21","slug":"dynamic-master-pages","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2008\/04\/02\/dynamic-master-pages\/","title":{"rendered":"Dynamic Master Pages"},"content":{"rendered":"<p>In ASP.net, there are multiple ways you can set dynamic master pages.\u00a0 They can be specified in the code behind or in the web.config file.\u00a0 Web.config may not sound like a dynamic option, but consider that the application does not need recompiling after a change to it.<\/p>\n<p>However, if you have code in those master pages, you&#8217;ll quick blow up at runtime.\u00a0 Even if your multiple master pages have the same method stubs.\u00a0 I found a good MSDN article dealing with the issue at <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/c8y19k6h.aspx\" title=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/c8y19k6h.aspx\" target=\"_blank\">http:\/\/msdn2.microsoft.com\/en-us\/library\/c8y19k6h.aspx.<\/a><\/p>\n<p>However, if you want the short version&#8230;<\/p>\n<p>1. Put a base class in AppCode that inherites from System.Web.UI.MasterPage and stub out any methods as virtual (overridable in vb.net).<\/p>\n<p>2. Inherit from that class in your master page class.\u00a0 SPECIAL NOTE:\u00a0 The example in the kb article above shows a master page with no code behind, so the Inherit property in the page tag specifies the base class.\u00a0 If you use a code behind like most sane developers, just specify the inherit of the base class in the code behind, and put the actual page in the inherit tag of the master page.\u00a0 See below.<\/p>\n<p>3. In pages that use the master page, do not specify a master page in the aspx.\u00a0 Do it in the webconfig, or in preinit.\u00a0 Put a mastertype tag with a typename property in the aspx specifying the base class.<\/p>\n<p>An illustration:<\/p>\n<p>the abstract master&#8217;s code behind:<br \/>\n<code>public class BaseMaster : System.Web.UI.MasterPage<br \/>\n{<br \/>\npublic overridable void TestFunction() {}<br \/>\n}<\/code><\/p>\n<p>the concrete master(s) look like this:<br \/>\n<code>&lt;%@ Master Language=\"C#\" AutoEventWireup=\"false\" CodeFile=\"MyMaster.master.cs\" Inherits=\"MasterPages_MyMaster\" &gt;<\/code><\/p>\n<p>The code behind of the master would work like this:<br \/>\n<code>public partial class MasterPages_MyMaster : BaseMaster<br \/>\n{<br \/>\npublic override void TestFunction()<br \/>\n{<br \/>\n\/\/ do something here ();<br \/>\n}<br \/>\n}<\/code><\/p>\n<p>examples of a web page, Test.aspx:<br \/>\n<code>&lt;%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\"Test.aspx.cs\" Inherits=\"Test\" %&gt;<br \/>\n&lt;%@ MasterType TypeName=\"BaseMaster\" %&gt;<\/code><\/p>\n<p>in the system.web section of web.config file:<br \/>\n<code>&lt;pages masterpagefile=\"~\/MasterPages\/MyMaster.master\"&gt;<\/code><\/p>\n<p>Now you can change the master page specified in the web config and not recompile your app.\u00a0 The site can have a whole new look and feel.\u00a0 You could change the master page to be determined in code and let a web site administrator pick the skin for the site.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In ASP.net, there are multiple ways you can set dynamic master pages.\u00a0 They can be specified in the code behind or in the web.config file.\u00a0 Web.config may not sound like a dynamic option, but consider that the application does not need recompiling after a change to it. However, if you have code in those master [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,24],"tags":[],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-aspnet","category-web"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}