Kelly Robins
Kelly Robins

Reputation: 7288

Overriding a master page function in ASP.net

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!

Upvotes: 1

Views: 8601

Answers (3)

Myke Black
Myke Black

Reputation: 1339

A much simpler way is to reference a master control directly from your child page.

In your master page, replace the sitepath() function with a literal control like so:

<div class ="breadcrumbs" runat="server"><asp:Literal id="litSitePath" runat="server" /></div>

then in the code for the master page use this:

BreadCrumbNav breadNav = new BreadCrumbNav(); 
breadNav.divClass = ".dv";    
breadNav.homeTitle = "ABC Home";    
litSitePath.Text = breadNav.Build();

and in the child page you can override this by using the Master.FindControl method like this:

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");    
Literal litSitePath = (Literal)Master.FindControl("litSitePath");
litSitePath.Text = breadNav.Build();

Easy peasy and no messing around with eventhandlers or interfaces or anything like that.

Upvotes: 1

Pauli &#216;ster&#248;
Pauli &#216;ster&#248;

Reputation: 6916

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.

Upvotes: 1

JoshJordan
JoshJordan

Reputation: 12975

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)

Upvotes: 3

Related Questions