Jamie Ide
Jamie Ide

Reputation: 49291

How can I hide the SiteMapPath root node on home page?

How can I hide the root node in a SiteMapPath control when the user is on the root node page? For example, my breadcrumb trail on a child page is:

Home > Products > Hammers > Ball Peen

which is fine. But when the user is on the Home page, the SiteMapPath control displays

Home

which is useless clutter. I want to suppress displaying Home (the root node) when the user is on the home page. I have the SiteMapPath control in a master page. Also, I'm handling SiteMapResolve to set the querystrings in the nodes.

Upvotes: 1

Views: 17285

Answers (10)

Narender Singh
Narender Singh

Reputation: 1

ParentLevelsDisplayed=0 will help

<asp:SiteMapPath ID="SiteMapPath1" runat="server" PathSeparator="" 
        ParentLevelsDisplayed="0" >
        <RootNodeTemplate></RootNodeTemplate>            
    </asp:SiteMapPath>

Upvotes: 0

Sean
Sean

Reputation: 1228

I had mine plugged into my _Layout.cshtml and found the easiest solution was to wrap an If statement (with previously suggested logic) around the control rendering block and call it a day:

@if (SiteMap.CurrentNode != SiteMap.RootNode)
{
  @Html.MvcSiteMap().SiteMapPath()
}

Upvotes: 1

Eugene
Eugene

Reputation: 1057

The one of the right way to hide SiteMapPath root note in 3 easy steps:

  • Reference MasterPage from ContentPage

    Example:

    <%@ MasterType VirtualPath="~/Master.master" %>
    
  • Make SiteMapPath as Protected Internal in designer class

    Example:

    protected internal global::System.Web.UI.WebControls.SiteMapPath SiteMapPath1;
    
  • Hide it from ContentPage

    Example:

    Master.SiteMapPath1.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);
    

Upvotes: 1

Matej
Matej

Reputation: 1

Recently I had similar problem, but I am using XmlDataSource for the menu in my solution .

Sample structure of the source XML:

<root>
  <Menu text="" url=""/>
  <Menu text="" url=""/>
</root>

If you want to NOT display 'root' menu item, you have to simple set XPath property on the XmlDataSource to value '/root/*'

Upvotes: 0

longbo567
longbo567

Reputation: 11

On the home page, add the script below to the "head" part:

protected void Page_Load(object sender, EventArgs e)
{
    SiteMapPath sp = (SiteMapPath)Master.FindControl("SiteMapPath1");
    sp.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);
}

To apply the above method, SiteMapPath1 should be placed on the MasterPage.

Upvotes: 1

CScott
CScott

Reputation: 71

There is also a ShowStartingNode property on the SiteMapDataSource. Set this to false to hide the root node.

Upvotes: 1

Tarik
Tarik

Reputation: 81781

<asp:SiteMapPath ID="contentNavigation" runat="server">
    <RootNodeTemplate>
    </RootNodeTemplate>
</asp:SiteMapPath>

and css code :

#ctl00_contentNavigation span:nth-child(2),span:nth-child(3)
{
    display:none;
}

Upvotes: 0

nc1943
nc1943

Reputation: 31

I've seen some code-based examples, but here's a cheep CSS solution (your target browsers must support css 2.1 though) that will hide the root node and the following path-separator.

Kill the Root node by setting the RootNodeTemplate to empty like so:

<asp:SiteMapPath ID="SiteMapPath1" runat="server" CssClass="breadCrumbTrail">
   <RootNodeTemplate></RootNodeTemplate>
</asp:SiteMapPath>

That will make it render nothing for the Root node, but the Root's path-separator will still be displayed, so add these CSS selectors to your stylesheet (Important: notice I gave my SiteMapPath1 element a CssClass named 'breadCrumbTrail'):

.breadCrumbTrail
{
  font-size: small;
}

/*
First child element rendered by a SiteMapPath is an <a> tag you have no control over, 
adjacent to that is your root node's span tag, adjacent to that is the root node's 
path-separator span: don't display it.
*/
.breadCrumbTrail > a:first-child + span + span
{
  display: none;
}

Upvotes: 3

Jamie Ide
Jamie Ide

Reputation: 49291

I managed to figure this out but it took a while because the problem I was having was somewhat subtle. schou-rode has the right idea and that is what I was doing in Page_Load without success. The reason it wasn't working is because I was cloning the node in SiteMapResolve and returning the clone. This occurred before Page_Load so SiteMap.CurrentNode referenced the clone and the comparison to SiteMap.RootNode failed.

Here's the full solution:

protected void Page_Load(object sender, EventArgs e)
{
    SiteMapPath1.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);
}

private SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
{
    if (SiteMap.CurrentNode == null || SiteMap.CurrentNode == SiteMap.RootNode)
    {
        return SiteMap.CurrentNode;
    }
    // clone and set querystring in other nodes...
}

Upvotes: 2

J&#248;rn Schou-Rode
J&#248;rn Schou-Rode

Reputation: 38346

One possible solution would be to simply hide the SiteMapPath control on the home page:

mySiteMapPath.Visible = (SiteMap.CurrentNode != SiteMap.RootNode);

Upvotes: 13

Related Questions