Nested Master Pages...are you sure?

Posted by: Clarity Blogs: ASP.NET, on 02 Mar 2006 | View original

I came across a pretty simple issue this week that a lot of developers tend to over-engineer.  Many folks are using nested master pages or multiple, separate master pages to manipulate their asp.net 2.0 web app layouts.  We were developing a login screen and welcome page (header, footer, one content column), leading to the meat of the app, with a more complex layout (header, top nav bar, side nav bar, content area, footer).  These all needed to be branded using a common theme.  Master Pages, baby!  Ah, but what about handling the differing layouts?

Nested master pages (to contain our nav bars) open a can of worms if you are developing using the Forms Designer of Visual Studio 2005; content forms inside of nested master pages will work beautifully in Source View or runtime, but cannot be displayed using the Designer view, so we've lost a nice chunk of .Net functionality.  There are some ways to override this and force the form to show in designer, but this article is encouraging simplicity in function, not workarounds.

So just maintain two Master Pages, right?  But that exposes an annoying, bug-probable architecture: if a change is made to one master page, but is forgotten in the second, we've lost some of the global branding of the app.  Plus, this just FEELS goofy, doesn't it?

I'm all for using either of these models if the situation presents itself, but our very common example, a branded login and entry page show how to save yourself from maintaining nested or multiple master pages: overriding the ContentPlaceHolder controls.

<body>
    
<form id="form1" runat="server">
    
    
<div id="wrap">
        
<div id="header">
            My Header Content
        
</div>
        
        
<asp:ContentPlaceHolder ID="NavbarPlaceHolder1" runat="server">
           Upper Navigation Section
           
<!-- This will be overridden -->
        
</asp:ContentPlaceHolder>
        
        
<asp:ContentPlaceHolder ID="SidebarPlaceHolder1" runat="server">
         Side Navigation Section
           
<!-- This will be overridden -->
        
</asp:ContentPlaceHolder>
     
        
<div id="main">
         
<asp:ContentPlaceHolder ID="MainContentPlaceHolder1" runat="server">
            Main Content
           
</asp:ContentPlaceHolder>
        
</div>
    
        
<div id="footer">
           
<asp:ContentPlaceHolder ID="FooterPlaceHolder1" runat="server">
            Footer Content
           
</asp:ContentPlaceHolder>
      
</div>
   
</div>
   
</form>

</body>

In the master page's ContentPlaceHolders, I would insert content and/or web user controls to format the website, including nav bar controls inside of these ContentPlaceHolders.  Then when we create content pages for the login and entry pages (the ones with no nav bars), and simply include blank ContentPlaceHolder references for the sections we want to hide, voila, they are hidden.  As the Southsiders say, "HE GONE".  Here's my fully branded login page, with no unwanted nav bars:

<asp:Content ID="ContentMain" ContentPlaceHolderID="MainContentPlaceHolder1" Runat="Server">
    
<div id="LoginWrapper">
        
<uc1:loginControl ID="myLoginControl" runat="server" EnableTheming="True" />
    </
div>
</asp:Content>
<asp:Content ID="Sidebar" ContentPlaceHolderID="SidebarPlaceHolder1" runat="server">
</asp:Content>
<asp:Content ID="TopNavbar" ContentPlaceHolderID="NavbarPlaceHolder1" runat="server">
</asp:Content>
When you create content pages that require the nav bars, simply do not override the ContentPlaceHolder, and the default content you added to the Master Page inside the nav bar ContentPlaceHolders will shine on through.

Advertisement

Similar Posts

  • Recipe: Dynamic Site Layout and Style Personalization with ASP.NET more
  • Atlas and the xhtmlConformance web.config key more
  • Master Pages and Content Pages in WSS 3.0 more
  • Master Page Performance more
  • Create Unlimited Subdomains with HTTP Modules more
  • Code Blocks Inside Master Pages Cause Trouble more
  • Working with Data in ASP.NET 2.0 more
  • Getting access to the Head tag in Master Pages more
  • Capture Master Page Events in Content Pages - ASP.NET 2.0 more
  • Master Page Inheritance and User Controls more

News Categories

.NET | ADO.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Community Server | dasBlog | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD