MasterPage using Masterpages, and inheritance

asp.netmaster-pages

I've got a "Master" Masterpage, and I've got 2 "Sub" Masterpages. Those 2 subs have their masterpage property set to the "Master" Masterpage. All good so far.

Well, I was told today to also make Sub1 and Sub2 actually "inherit" from the Master Masterpage. I thought they were being imprecise with their language, but no, she wanted me to change the Class definition to

public partial class Sub2 : TheMaster 

(those aren't the names).

So, now I've got Sub1 and Sub2 that are master pages, and have a master page of TheMaster, and ALSO they are of the class of "TheMaster"

I'm quite confused by this, and not sure what kinds of effects this will have. It seems to compile and run fine right now, but we are just starting this 6 month project today, and I don't want to get to month 5 and find out that we have a major design flaw.

Can anyone tell me that this is totally fine, or that we are completely messed up?

I'm scared…hold me.

EDIT: Clarification — the reason she wanted me to also inherit from the master master page is that we set some things in that page, and we want them available to the sub master pages. Things like the current node in the sitemap that the rendered page is, some user account stuff, and much more.

Best Answer

It is indeed possible to have nested master pages - see http://msdn.microsoft.com/en-us/library/x2b3ktt7.aspx for a reference.

EDIT as per comment

I don't believe your sub master page should be inheriting from the main master page in the code behind.

Each master page, including the sub master pages should inherit directly from MasterPage, i.e. public partial class Sub1 : System.Web.UI.MasterPage.

Only the ASP markup of the sub master page should be referencing the main master page, i.e. <%@ Master Language="C#" MasterPageFile="~/TheMaster.master" ... />

If you add your sub master page to the project via the VS UI, selecting TheMaster.master as the master page then you will see that this how things are set up. Master page usage is designed to be only via content (markup), and not via class inheritance.

Related Topic