Click here to Skip to main content
Licence 
First Posted 3 May 2007
Views 34,999
Downloads 156
Bookmarked 22 times

Inheritance and Nested Master Pages

By | 10 May 2007 | Article
An article is about editing inherited properties of top level master page from .aspx code

Introduction

This article is about small problem I got stuck when I tried to use inheritance with Nested Master Pages. If you looking for something general about Master Pages please try to see this article Inside Master Pages

Problem description

Well, here is my situation

Screenshot - NestedMasterPages.gif

As you can see I try to inherit Child.Master from Main.Master and then use Child.Master in MyPage.

Main.master

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head id="mainHead" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="mainForm" runat="server">
        <asp:Literal ID="TopMessageLiteral" runat="server" />
        
        <div id="content">           
            <asp:contentplaceholder id="cphMain" runat="server" />
    </div>
    </form>
</body>
</html>

Child.master

<%@ Master Language="C#" MasterPageFile="~/Master/Main.master" AutoEventWireup="true" 
    CodeBehind="Child.master.cs" Inherits="Master.Child" %>
<%@ MasterType VirtualPath="~/Master/Main.master" %>
<asp:Content ID="cntMain" ContentPlaceHolderID="cphMain" Runat="Server">
    <div id="left">       
        <asp:contentplaceholder id="cphLeftCol" runat="server" />
    </div>  
    
    <div id="right">
        <asp:contentplaceholder id="cphRightCol" runat="server" />    
    </div>
</asp:Content>

MyPage

<%@ Page Language="C#" MasterPageFile="~/Master/Child.master" AutoEventWireup="true" 
    Inherits="MyPage" Title="Untitled Page" Codebehind="MyPage.aspx.cs" %>
<%@ MasterType VirtualPath="~/Master/Child.master" %>

<asp:Content ID="cntLeftCol" ContentPlaceHolderID="cphLeftCol" Runat="Server">
    <p>test left</p>
</asp:Content>
<asp:Content ID="cntRightCol" ContentPlaceHolderID="cphRightCol" Runat="Server">
    <p>test right</p>
</asp:Content>

Code-behinds please see below:

//Main.master
//-------------
public partial class Main : System.Web.UI.MasterPage {
  
  private string _topMessage;

  public string TopMessage {
    get { return _topMessage; }
    set { _topMessage = value; }
  }

  protected void Page_Load(object sender, EventArgs e) {
    TopMessageLiteral.Text = TopMessage;
  }
}

//Child.master 
//------------
public partial class Child : Main {

   protected new void Page_Load(object sender, EventArgs e) {
               
   }

}

//MyPage.aspx 
//----------
public partial class MyPage: System.Web.UI.Page {

  protected void Page_Load(object sender, EventArgs e) {
    Master.TopMessage = "My top message";
  }
}
It looks like good code but it doesn't work. My TopMessage will never be shown. If you try to debug this code you'll see that when you instantiate TopMessage property in MyPage.Page_Load all is going well and value is successfully saved to _topMessage variable. But when it will be read from Main.Page_Load it is empty. Frankly, I don't know why. I have some guesses only. But it seems I found solution.

Solution

The trick is to use Context.Items collection.
//Main.master
//-------------
public partial class Main : System.Web.UI.MasterPage {
  
  public string TopMessage {
    get {
       if (Context.Items["_topMessage"] != null)
          return Context.Items["_topMessage"].ToString();
       else
          return string.Empty;
    }
    set {
        Context.Items["_topMessage"] = value;
    }
  }

  protected void Page_Load(object sender, EventArgs e) {
    TopMessageLiteral.Text = TopMessage;
  }
}
In this way TopMessage value will never be lost.

One More Solution

Suggested by Irwan Hassan technic need not Main.master <- Child.master inheritance

//MyPage.aspx 
//----------
public partial class MyPage: System.Web.UI.Page {

  protected void Page_Load(object sender, EventArgs e) {
    Master.Master.TopMessage = "My top message";
  }
}

History

May 09, 2007: just add a piece of code to play experiments

May 10, 2007: add one more solution

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Bobac

Web Developer

Ukraine Ukraine

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThere's a built-in way to achieve this: PinmemberJim Raley7:03 9 Dec '08  
GeneralA 5 from me! PinmemberAndrei Rinea6:14 12 Oct '07  
GeneralTry Master.Master.TopMessage = ... PinmemberIrwan Hassan7:23 9 May '07  
GeneralRe: Try Master.Master.TopMessage = ... PinmemberBobac2:54 10 May '07  
GeneralRe: Try Master.Master.TopMessage = ... PinmemberIrwan Hassan3:51 10 May '07  
GeneralEvent firing sequence PinmemberBrian Lowe22:05 7 May '07  
AnswerRe: Event firing sequence PinmemberBobac13:19 8 May '07  
GeneralRe: Event firing sequence PinmemberBrian Lowe3:43 9 May '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 10 May 2007
Article Copyright 2007 by Bobac
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid