Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Hide Pages from Menu Control

0.00/5 (No votes)
8 Jun 2009 1  
Here is how you easily hide specific pages from the Menu control when combined with the SiteMapPath control

Introduction

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 string of text to the description attribute, like this, where we want to hide the SearchResults.aspx page from the menu. Notice that we're using the string Hidden in the description attribute.

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
    <siteMapNode title="Home" description="..." url="~/Default.aspx">
        ...

        <siteMapNode title="Search Results" description="Hidden"
             url="~/SearchResults.aspx" />
    </siteMapNode>
</siteMap>

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:

/// <summary>
/// Removes various menu items that must be present in the sitemap file to correctly 
/// display the breadcrumb using the sitemappath control. SiteMapNodes in the sitemap 
/// file must have the Description property set to Hidden (case sensitive).
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void mainMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
    // Hidden?
    if (((SiteMapNode)e.Item.DataItem).Description == "Hidden")
    {
        mainMenu.Items.Remove(e.Item);
    }
}

Simple, but effective.

History

  • 8th June, 2009: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here