Hide Pages from ASP.NET Menu control


When you use the Menu control with the SiteMapDataSource control and an associated XML sitemap file, and also use the SiteMapPath control for creating the bread crumb, you often need to include specific pages in the sitemap file, but hide the same pages from the menu. If you don’t include a page in the sitemap file, it isn’t shown in the menu, which is what you want, but when you then browse to that specific page, your breadcrumb disappears. The option is to include the page in the sitemap file, but add a custom attribute, like this, where we want to hide the SearchResults.aspx page from the menu. Notice that we’ve added the HideFromMenu attribute with a value of true.

xml version="1.0" encoding="utf-8" ?>

        ...
        true"
             url="~/SearchResults.aspx" />


Now, that doesn’t quite do it, as now the page is shown in both the breadcrumb, but also in the menu. However, if you add a little code to the MenuItemDataBound event of the Menu control, like this, the menu item is removed:

Visual Basic

'''
 ''' Removes various menu items that must be present in the sitemap file to correctly ''' display the breabcrumb using the sitemappath control. SiteMapNodes in the sitemap ''' file must have the custom HideFromMenu property set to true. ''' 

'''
'''
Protected Sub MainMenu_MenuItemDataBound(ByVal sender As Object, ByVal e As MenuEventArgs) Handles MainMenu.MenuItemDataBound
    ' Get current node
    Dim currentSiteMapNode As SiteMapNode = CType(e.Item.DataItem, SiteMapNode)

    ' Has the HideFromMenu property/attribute and value been specified?
    If Not String.IsNullOrEmpty(currentSiteMapNode.Item("HideFromMenu")) Then
        Dim hide As Boolean

        ' Get value from property
        If Boolean.TryParse(currentSiteMapNode("HideFromMenu"), hide) Then
            ' Hide menu item?
            If hide Then
                ' Top-level menu?
                If e.Item.Depth > 0 Then
                    ' Remove menu item
                    e.Item.Parent.ChildItems.Remove(e.Item)
                Else
                    ' Remove menu item
                    CType(sender, Menu).Items.Remove(e.Item)
                End If
            End If
        End If
    End If
End Sub

Visual C#

/// 
 /// Removes various menu items that must be present in the sitemap file to correctly /// display the breabcrumb using the sitemappath control. SiteMapNodes in the sitemap /// file must have the custom HideFromMenu property set to true. /// 

///
///
protected void mainMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
    // Get current node
    SiteMapNode currentSiteMapNode = e.Item.DataItem as SiteMapNode;

    // Has the HideFromMenu property/attribute and value been specified?
    if (!String.IsNullOrEmpty(currentSiteMapNode["HideFromMenu"]))
    {
        bool hide;

        // Get value from property
        if (bool.TryParse(currentSiteMapNode["HideFromMenu"], out hide))
        {
            // Hide menu item?
            if (hide)
            {
                // Top-level menu?
                if (e.Item.Depth > 0)
                {
                    // Remove menu item
                    e.Item.Parent.ChildItems.Remove(e.Item);
                }
                else
                {
                    // Remove menu item
                    ((Menu) sender).Items.Remove(e.Item);
                }
            }
        }
    }
}
Advertisements