开发者

Using SiteMapPath to create a dynamic page title?

开发者 https://www.devze.com 2023-02-12 03:24 出处:网络
I开发者_Python百科 currently use SiteMapPath to generate a breadcrumb for my ASP.net 3.5 (vb.net) pages and it works great.

I开发者_Python百科 currently use SiteMapPath to generate a breadcrumb for my ASP.net 3.5 (vb.net) pages and it works great.

Now I am trying to figure out how I might be able to use the same control to build a dynamic page title (in the tag). I want a reverse path listed, but the SiteMapPath control includes links and bunch of styling spans. Is there any way to remove all of that, and just get a plain path with separators?

For example, Let's say we are on the "Press Releases" page inside of the "About" section of my site.

The breadcrumb shows up as:

Home > About > Press Releases

I want to have the page title be:

Press Releases - About - Company Name

So I need it to reverse the order, drop all spans, links and styling (since this is inside the tag) and drop the root node "Home" and then add the company name to the end. Since my menu nav and breadrumbs are all driven from the sitemap file, I thought it would make sense to try to make the title do the same.

Any thoughts? Thanks.


The best way to achieve your desired output is to ignore the SitePath control, and instead use the SiteMap's SiteMapNode's collection. The server parses the web.sitemap into a collection of SiteMapNodes and wires up the SiteMap.CurrentNode by finding a node that matches the current page's URL. Each SiteMapNode has a ParentNode property. Here is the reference page on MSDN.

So, all you need to do is check if the CurrentNode has a parent, if it does you add the ParentNode's title to the CurrentNode's title and keep going until you reach the RootNode (where you substitute your company name for the root node's title).

Below is a quick solution; it could go in the MasterPage if you are using one. I'm not sure your language, but this should be easy to rewrite in VB.Net. I gave it a simple test and it seemed to work. You can customize the characters that separate the page titles.

protected void Page_Load(object sender, EventArgs e)
{
    Page.Title = SiteMapTitle(SiteMap.CurrentNode, "", " - ");
}

private string GetNodeTitle(SiteMapNode oNode)
{
    if (oNode == SiteMap.RootNode)
        return "Company Name";
    else
        return oNode.Title;
}

private string SiteMapTitle(SiteMapNode oNode, string szTitle, string szItemSeparator)
{
    if (szTitle != string.Empty)
        szTitle = szTitle + szItemSeparator + GetNodeTitle(oNode);
    else
        szTitle = GetNodeTitle(oNode);

    if (oNode.ParentNode != null)
        szTitle = SiteMapTitle(oNode.ParentNode, szTitle, szItemSeparator);

    return szTitle;
}

Hope that helps...

0

精彩评论

暂无评论...
验证码 换一张
取 消