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.
="1.0" ="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:
protected void mainMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
if (((SiteMapNode)e.Item.DataItem).Description == "Hidden")
{
mainMenu.Items.Remove(e.Item);
}
}
Simple, but effective.
History
- 8th June, 2009: Initial post