Showing posts with label Attribute. Show all posts
Showing posts with label Attribute. Show all posts

Wednesday, October 28, 2009

SiteMap Custom Attributes

In this example we will use a custom attribute to hide some SiteMapNodes from a Menu control.
You can create custom attributes in a sitemap file easily as follows:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode>
<siteMapNode url ="./Default.aspx" title="Find Equipment" description="Find Equipment" />
<siteMapNode url="./RentProductPage.aspx" title="Make Reservation" description="Make Reservation" />
<siteMapNode url="./SearchResults.aspx" title="Search Results" description="Search results" IsMenuVisible="false" />
<siteMapNode url="./ProductDetails.aspx" title="Product Details" description="Product Details" />
<siteMapNode url="./ManageEquipment.aspx" title="Manage Equipment" description="Manage Equipment" />
<siteMapNode url="./SampleContract.aspx" title="View Sample Contract" description="View Sample Contract" />
</siteMapNode>
</siteMap>





On the SearchResults.aspx node there is a custom attribute called “IsMenuVisible”. The “IsMenuVisible” attribute has been set to “False” since we do not want this node showing in the nav menu.



This attribute can easily be accessed from the MenuItemDataBound event in the codebehind as follows:




void navMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
SiteMapNode node = ((SiteMapNode) e.Item.DataItem);
MenuItem parentItem = e.Item.Parent;

string isMenuVisible = node["IsMenuVisible"];
if (isMenuVisible != null)
{
if (isMenuVisible.ToUpper() == "FALSE")
{
if (parentItem != null)
{
parentItem.ChildItems.Remove(e.Item);
}
else
{
navMenu.Items.Remove(e.Item);
}
}
}
}





In the above example we cast “e.Item.DataItem” back to a SiteMapNode and access the custom attribute through it’s string index.  The remaning code is used to remove it from the Menu