views:

985

answers:

2

In my master page I have the line

<div class ="breadcrumbs" runat="server"><%= SitePath()%></div>

SitePath() involks a custom class that builds navigation elements based on the combination of the static sitemap and dynamically generated pages. It returns the html for a custom breadcrumb navigation element.

Here is the SitePath() code from my code behind

public string SitePath()
{
    BreadCrumbNav breadNav = new BreadCrumbNav();
    breadNav.divClass = ".dv";
    breadNav.homeTitle = "ABC Home";
    return breadNav.Build();
}

I'd like to be able to override this from my dynamic pages so I can add to the path. For Example...

public override string SitePath()
{
    BreadCrumbNav breadNav = new BreadCrumbNav();
    breadNav.divClass = ".dv";
    breadNav.homeTitle = "ABC Home";
    breadNav.AddPage("Cooking Equipment", "PathyGoodness/Cooking+Equipment.ASPX");
    breadNav.AddPage("Toasters", "PathyGoodness/Cooking+Equipment/Toasters.ASPX");
    return breadNav.Build();
}

Is there a way to bring the master page methods into scope so I can override them- or do I need to go about this in a different way? It feels like I'm missing something really obvious, but I seem to be stuck.

Thanks for your help!

+2  A: 

Since the Master page is not actually inherited, but rather used as a template, this is not directly possible.

A few other approaches you could try:

Firstly, put this in a ContentPlaceholder and override it in the ASPX markup of your individual pages only where necessary.

A more complicated approach would be to create an interface for all your pages to inherit from that guarantees they have a SitePath() method. Call that method on the page instead, from the Master, and in those pages that do not "override" the behavior, simply call the Master. In others, add your specific implementation to that method, and voila! (I suppose you could also use an abstract BasePage-type class to call the Master from that method)

JoshJordan
Thank you very much for your help. This is exactly what I needed to know. I'm thinking for simplicity's sake that I'll expose a public method on the master page that lets me pass the additional path information from the content page- We'll see how that goes. Anyways again thanks!!
apocalypse9
+1  A: 

Generally im against putting page/context specific methods in the masterpage class. Why? Because, as Josh mentioned, Masterpages is just templates. You'll never inherit from a masterpage and you can't show a masterpage without also having access to a Page object. Therefor it makes more sense to put such methods in the Page class.

BurningIce
I'd tend to agree. I do like to let the master-page handle the nav elements. Usually it is trivial to add a sitemap based breadcrumb nav to the site, and for most of my pages that is sufficient. I didn't want to disrupt these pages by forcing extra code.The solution I ended up using was exposing an AddPath() method on the master page. This allows the dynamically created urlRewritten pages to register their path, without necessitating extra code for the other pages on the site.I can't say I'm 100% happy with the solution, but it seemed best given the situation.
apocalypse9
by putting the AddPath into, say, a baseclass which all you pages inherits from, you don't clutter the indivual pages with extra code.In the end its all a matter of style and preference since the outcome is the same. Im just agains putting logic into something that is ment only for appearance.1. Add the AddPath() and BuildPath() to your BasePage class2. Call this.AddPath() when necessary from your individual pages3. Call Page.BuildPath() from your MasterPage
BurningIce