MasterPage Tips


Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!

 

MasterPage Tips

Here we have some general tips on using the .NET Master Pages.

Master Pages and the ASP.NET Page Life Cycle

Master Pages and their content pages have their events fire off in the order of the ASP.NET Page Lifecycle with somewhat slightly bizarre ordering, well at least it seems that way until you think that the Master Page acts like a control. So for instance, if we take the 3 events from the life cycle of Init, Load and PreRender and see the order they fire off in, see here:


Master: OnInit
Content Page: OnInit
Content Page: OnLoad
Master: OnLoad
Content Page: OnPreRender
Master: OnPreRender


So, Master Page's Init event, then page Init, then Page Load, then Master Load, followed by page PreRender and finally Master PreRender. It does seem they should be in a more sequential order, but there you go. Here's the C# I used to get these results:

Master Page sample


using System;

namespace MasterContentLifeCycle
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected override void OnInit(EventArgs e)
        {
            Response.Write("Master: OnInit <br />");
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            Response.Write("Master: OnPreRender <br />");
            base.OnPreRender(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            Response.Write("Master: OnLoad <br />");
            base.OnLoad(e);
        }
    }
}


Content Page Sample


using System;

namespace MasterContentLifeCycle
{
    public partial class _Default : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            Response.Write("Content Page: OnInit <br />");
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            Response.Write("Content Page: OnPreRender <br />");
            base.OnPreRender(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            Response.Write("Content Page: OnLoad <br />");
            base.OnLoad(e);
        }
    }
}


Nesting Master Pages

It's possible to nest multiple layers of master pages together, quite handy if you have an outer shell in your site design, and then sub-sections inside the site design which are different per section but still share the same outer shell. Here's the ASPX code which shows how to do this. Starting with TopLevelMaster.Master, it then nests the NestedMasterPage1.Master, followed by the content page WebForm1.aspx:

Top Level Master Page (TopLevelMaster.Master)


<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="TopLevelMaster.master.cs" Inherits="MasterContentLifeCycle.TopLevelMaster" %>

...

Top Level Master Page<br />

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>


Nested Level Master Page (NestedMasterPage1.Master)


<%@ Master Language="C#" MasterPageFile="~/TopLevelMaster.Master" AutoEventWireup="true" CodeBehind="NestedMasterPage1.master.cs" Inherits="MasterContentLifeCycle.NestedMasterPage1" %>

...

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

Nested level Master Page<br />

<asp:ContentPlaceHolder ID="ContentPlaceHolder_1" runat="server"> </asp:ContentPlaceHolder>

</asp:Content>


Content Page(WebForm1.aspx)


<%@ Page Title="" Language="C#" MasterPageFile="~/NestedMasterPage1.master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MasterContentLifeCycle.WebForm1" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_1" runat="server">

Page content <br />

</asp:Content>


This produces the following HTML output:


Top Level Master Page
Nested level Master Page
Page content