German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


How to add icons to a SiteMap Menu in ASP .NET

by Omer kamal Jun 22, 2006
This article is explaining how can one add icons to a menu which was created through SiteMapDatasource object.

I think it is very easy to bind a Menu to a SiteMapDataSource , it can prove challenging to find how to extend the sitemap with custom properties and use these extended properties in a Menu. This Article shows how to add icons in a sitemap-driven menu.

SiteMap allows the addition of custom attributes to your site map nodes. If you're using the XML site map provider, it's as simple as this:

<siteMapNode url="default.aspx" title="Home"  description="The home page" imageUrl="home.gif">

The first idea that comes to mind to use these custom attributes is to just replace the automatic bindings you get from binding a Menu to SiteMapDataSource with "manual" bindings. This works very well with XmlDataSource because it is implemented so that the nodes implement ICustomTypeDescriptor , so any XML attribute is exposed as a property on the node as far as reflection is concerned. This is a very powerful feature of .NET reflection that gives it some of the qualities of dynamic languages. Unfortunately, SiteMapDataSource does not implement this, nor does Menu know how to query custom site map attributes. This is an oversight and we may add that support in future releases.


An easy (although not declarative) way out of this problem is to hook the OnMenuItemDataBound event and to set the custom properties from there:

public void OnItemBound(object sender, MenuEventArgs args) {
    args.Item.ImageUrl = ((SiteMapNode)args.Item.DataItem)["imageUrl"];
}


Have fun codding!


The Source code of this above article can downloaded from www.Gotdotnet.com Website.

Here's a link to the full source code:


Download the source code
Visitors/Readers Comments
(for questions please use The Forum)



Ralph Basile

I am attempting to use Microsoft Expression Web to accomplish something like this, but am stumpted.

I actually only want one image - the current node image - to appear. This would be the image listed in the web.sitemap "imageurl" value. How would I do this?

 

04/09/2007 10:22:27 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)