Click here to Skip to main content
Licence CPOL
First Posted 8 Jun 2009
Views 6,144
Bookmarked 15 times

Hide Pages from Menu Control

By integrasol | 8 Jun 2009
Here is how you easily "hide" specific pages from the Menu control when combined with the SiteMapPath control

1

2

3
2 votes, 100.0%
4

5
4.00/5 - 2 votes
μ 4.00, σa 1.10 [?]

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

integrasol

Team Leader
Integrated Solutions
Denmark Denmark

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 8 Jun 2009
Article Copyright 2009 by integrasol
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid