How to hide current node in asp:SiteMapPath?

735 Views Asked by At

I need to hide current node in custom bread crumb on master page. I'm using SiteMapPath control which is already customized. I'm taking custom sitemap from web.sitemap file and all is configured in web.config properly.

How to do it?

2

There are 2 best solutions below

0
On BEST ANSWER

Thanks for your self-answer, it helped me get started and then I tweaked it and think this code is a little cleaner. Though I didn't analyze it deeply, I think it will run faster since it will only run once when the final node (SiteMapNodeItemType.Current) is bound whereas your code is iterating through that loop each time the event is fired.

protected void Breadcrumb_ItemDataBound(object sender, SiteMapNodeItemEventArgs e)
{
   // If this is the current node, hide it along with its 
   // separator (both have same ItemIndex)
   if (e.Item.ItemType == SiteMapNodeItemType.Current)
   {
      foreach (SiteMapNodeItem node in (from SiteMapNodeItem x in ((SiteMapPath)sender).Controls
                                        where x.ItemIndex == e.Item.ItemIndex
                                        select x).ToList())
         node.Visible = false;
   }
}
0
On

Additional problem occurs when I discovered that when I hide last child, separator will stay visible.

I've found some solution:

I will use OnItemDataBound

definition: <asp:SiteMapPath OnItemDataBound="SiteMapNodeItemEventHandler" /> implementation: <asp:SiteMapPath ID="siteMapPath" runat="server" SiteMapProvider="CustomSiteMapProvider" OnItemDataBound="SiteMapPath_OnItemDataBound">

cs part:

protected void SiteMapPath_OnItemDataBound(object sender, SiteMapNodeItemEventArgs e)
    {
        //Hiding current bread crumb node - it will be presented via ajax after page load.
        SiteMapNodeItem nodeItem = e.Item;
        SiteMapNode node = ((SiteMapPath)sender).Provider.CurrentNode;

        // need to hide separator also, so I'm looking for current node index to compare to SiteMapNodeItem.ItemIndex
        // same index will be for node and separator.
        if ((node != null) && (node.ParentNode != null))
        {
            int index = 0;
            do
            {
                node = node.ParentNode;
                index++;
            } while (node.ParentNode != null);

            if (nodeItem.ItemIndex == index)
            {
                nodeItem.Visible = false;
            }
        }
    }