A cleaner and better way (my opinion!) would be to, sometimes, replace some of the System.Web.UI.WebControls.ContentPlaceHolder controls from my master page with empty content and by this achieving the same goal as with the multiple master pages approach. The problem with this solution is that is seems impossible to work with existing ASP.NET Content controls from code behind. Even if I set IDs to the controls I can't seem to access them from code. I found a simple solution to this by creating a new, empty ITemplate and using the Page.AddContentTemplate function:
public class StandardPage : Page { protected override void OnPreInit(System.EventArgs e) { // If the param showStripped is submitted we want to hide // header and footer by adding empty content controls overriding // ContentPlaceholders for those areas in the MasterPage var showStripped = Request.Params["showStripped"]; if (!string.IsNullOrEmpty(showStripped)) { AddContentTemplate("cphFacebookScript", new EmptyContent()); AddContentTemplate("cphHeader", new EmptyContent()); AddContentTemplate("cphFooter", new EmptyContent()); } base.OnPreInit(e); } } public class EmptyContent : ITemplate { void ITemplate.InstantiateIn(Control container) { } }
So when calling the page with the querystring showStripped we will add empty content templates to some of the content placeholders from the master page. Pretty simple, right?! :)