MasterPage using Masterpages, and inheritance
Asked Answered
D

3

7

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.

Deiform answered 2/6, 2009 at 21:38 Comment(2)
It's a bit strange that you have a masterpage in a masterpage. The masterpage usually contains the main <html>, <head> and <body> tags. Does your sub-masterpage contain those too?Layamon
Nope. Main page contains all that you mentioned, plus a header and a footer div, that are constant over the whole site. Sub master 1 contains a layout for 3 columns in the content body, and sub master 2 contains a 2 column layout (and some other things, really)Deiform
A
1

Basically what you have done is setup the page so that the class inherits all of the items from the specified base. The hookup of the "Masterpage" property, creates a weak association, and renders content inside of a specific structure. The class inheriting from the base moves the logic forward, into a manner where rather than just having it execute, you can override it.

Now, after thinking about it, you most likely do not want the MasterPage property set....but just the class inheritance

Andreas answered 2/6, 2009 at 21:44 Comment(2)
Okay, but why? And how would I specify content areas in the master master page, and then use those areas and specify new content areas in the sub master pages if I don't want the master page property set?Deiform
Overall, it depends onwhat you are trying to do, if the sub pages need to override method functionality of the parent, then you are in a bit of a trickly situation. If you just need the info, make the items public on the main page, and reference this.parent to get to them.Andreas
W
5

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.

Wont answered 2/6, 2009 at 21:45 Comment(2)
I know it is possible. I stated that in my post. I told you how I have it setup and said how it was working. My questions was more toward SHOULD I be both specifying a master page and inheriting from the same master page?Deiform
+1 for "Master page usage is designed to be only via content (markup), and not via class inheritance." That helped me move away from wasting more time on trying to fit class logic into Master Pages.Mirellamirelle
L
2

I just ran into this, and wanted to add specific problems you'll run into when inheriting from master pages you're also nesting. For example, the following code in BaseMaster.master.cs:

//BaseMaster.master.cs
public String FooLabelText
{
    get { return FooLabel.Text; }
    set { FooLabel.Text = value; }
}

when called from your inherited page will throw a NullReferenceException. FooLabel (a label in your BaseMaster.master file) doesn't exist in the nested master page (SubMaster.master) so if you access that property it will break.

You could get around this by overridding the property in your subclass and re-directing it to the master page you're nested in, but this really breaks the design philosophy of .NET master pages:

//SubMaster.master.cs inherits from BaseMaster
new public String FooLabelText
{
    get { return (Master as BaseMaster).FooLabel.Text; }
    set { (Master as BaseMaster).FooLabel.Text = value; }
}

Note that I'm not using base.FooLabel, but Master.FooLabel, which is really pointing to a different object. At best, this is a hack, and in many cases it's going to create more work and duplication.

One alternative would be to create a virtual master page base class with the desired application functionality, and have each master page inherit from this class. I can only see very limited advantages to using this over using a virtual page base class, and many downsides. It's probably best to use master pages for what they're best at, reducing replication in your markup, and put the rest of the required functionality in your page classes.

Leninism answered 8/12, 2010 at 14:8 Comment(0)
A
1

Basically what you have done is setup the page so that the class inherits all of the items from the specified base. The hookup of the "Masterpage" property, creates a weak association, and renders content inside of a specific structure. The class inheriting from the base moves the logic forward, into a manner where rather than just having it execute, you can override it.

Now, after thinking about it, you most likely do not want the MasterPage property set....but just the class inheritance

Andreas answered 2/6, 2009 at 21:44 Comment(2)
Okay, but why? And how would I specify content areas in the master master page, and then use those areas and specify new content areas in the sub master pages if I don't want the master page property set?Deiform
Overall, it depends onwhat you are trying to do, if the sub pages need to override method functionality of the parent, then you are in a bit of a trickly situation. If you just need the info, make the items public on the main page, and reference this.parent to get to them.Andreas

© 2022 - 2024 — McMap. All rights reserved.