Saturday, August 21, 2010

Using ImageUrl in sitemap for menu

For example, you could have:

The imageUrl property shown here is a custom attribute and gets added to the attributes of the SiteMapNode, which can be accessed within the MenuItemDataBound event; this is called for every menu item as it's bound to the underlying siteMapData. So you could have this:

protected void menu1_MenuItemDataBound(object sender, MenuEventArgs e)
{
SiteMapNode node = e.Item.DataItem as SiteMapNode;

if (!string.IsNullOrEmpty(node["imageUrl"]))
e.Item.ImageUrl = node["imageUrl"];
}

To access the attributes you just index into the default collection on the SiteMapNode.

the advantage of this approach is twofold; you don't pollute your title with HTML hacks, and you keep the image data as a separate item, alongside the other site map data.

No comments:

Post a Comment