views:

67

answers:

1

I have a solution with quite a few different MasterPages / BasePages which all inherit from somewhere else. My problem is that I have a virtual string in BaseMaster, which is overridden by BaseManagement, but when I try to access this string I always get the base value

The point of inheriting masters and pages is obviously to avoid having duplicate code everywhere.

Maybe it has something to do with the fact that I "need" protected void Page_Load(object sender, EventArgs e) at every master no matter


Base Classes

So, here's an snippet from Base.Master.cs

public abstract partial class BaseMaster : MasterPage, IRewritablePageElement
{
    public BasePage BasePage { get { return Page as BasePage; } }
    public BaseMaster Self { get { return (BaseMaster)this.Page.Master; } }
    public virtual string accessUri { get { return "/"; } }
    public string AccessUri { get { return Self.accessUri; } }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.OnPageLoad(sender, e);
    }

    /// <summary>
    /// If we want to do something on page load, we override the following method
    /// </summary>
    public virtual void OnPageLoad(object sender, EventArgs e)
    {
        SetCacheability();

        RedirectSiteDown();
        RedirectUnregisteredUsers();
        RedirectUnprivilegedUsers();

        if(!IsPostBack)
        {
            PopulateMenu();

            ...

As you already noticed,

    public BaseMaster Self { get { return (BaseMaster)this.Page.Master; } }
    public virtual string accessUri { get { return "/"; } }
    public string AccessUri { get { return Self.accessUri; } }

is as ugly as it gets, since it should just be

    public virtual string AccessUri { get { return "/"; } }

but the code somehow manages to get all the way down to a level where AccessUri is "/" even though it has been overriden with "/a/" somewhere at a higher level:

public partial class BaseManagementMaster : BaseMaster
{
    public override string accessUri { get { return "/a/"; } }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

If not for the Self thingy, here AccessUri is still "/". which makes absolutely no sense. Also, the fact that I have to re-declare Page_Load methods makes very little sense too.

Is there a clean way to do master page inheritance, is it even realistically possible?

A: 

the problem was in inheriting masterpages like

BaseManagementMaster : BaseMaster

this is wrong, the correct way to do this should be to just declare MasterPages, and then have abstractions at Page level, not Master level.

Nico
I have to disagree (and sorry that I didn't see this over the weekend) because I use inherited masterpages on my project. I also use inherited page classes. You could have inherited a base class instead of a base page you know.
drachenstern