Jeff Cleath

Technology Management

Adding MouseOver Images to the ASP.NET 2.0 Menu Control

 

When using the ASP.NET 2.0 Menu control I was surprised to find that it did not support an image mouseover event to swap images.   After doing a Google search and finding the post by Stephen Page at http://www.velocityreviews.com/forums/t117199-menu-control-aspnet-20.html I was able to use his example and develop the following code to implement the functionality.  I added the custom attributes of ImageUrl and AltImageUrl to the sitemap file and then used the following ItemDataBound event on the menu control.

protected void _siteMenu_ItemDataBound(object sender, System.Web.UI.WebControls.MenuEventArgs e)

{

// Reference the underlying SiteMapNode object...
MenuItem item = (MenuItem)e.Item;
SiteMapNode nodeFromSiteMap = (SiteMapNode)e.Item.DataItem;

string onImage = "";
string offImage = "";
string navUrl = "";

// If we have an imageUrl value, assign it to the menu node's ImageUrl property
if (nodeFromSiteMap["imageUrl"] != null)
{
onImage = System.Web.
HttpContext.Current.Request.ApplicationPath + System.IO.Path.Combine("/Images/Elements/", nodeFromSiteMap["imageUrl"]);
offImage = System.Web.
HttpContext.Current.Request.ApplicationPath + System.IO.Path.Combine("/Images/Elements/", nodeFromSiteMap["altImageUrl"]);
}
navUrl = nodeFromSiteMap[
"url"];

// These objects are necessary in order to capture the image object into a rendered html format
string src = offImage;
string toolTip = "";
System.Text.
StringBuilder sb = new System.Text.StringBuilder();
System.IO.
StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htmWriter = new HtmlTextWriter(sw);
HtmlImage image = new HtmlImage();

image.Style.Add("border-style", "none");
MenuItem theMenuButton = new MenuItem();
theMenuButton.NavigateUrl = navUrl;
image.Src = src;

if (onImage != "" && onImage != null)
image.Attributes[
"onMouseOver"] = "this.src='" + onImage + "';";
if (onImage != "" && onImage != null)
image.Attributes[
"onMouseDown"] = "this.src='" + onImage + "';";
image.Attributes[
"onMouseOut"] = "this.src='" + offImage + "';";
image.RenderControl(htmWriter);
item.Text = sw.ToString();
}

If anyone else has other options I would be glad to hear back from you. 

Comments

oooshola said:

This seems like a pretty clean way to do it. However, I can no longer see my dynamic menu items appear and disappear. Did i do something wrong?

# June 14, 2007 10:18 AM

Vimal said:

Hi,

I am also in need of such a component. Let me try this

# November 5, 2007 11:53 PM

sulfur_scratch said:

Could this have not been achieved using CSS?

# December 20, 2007 4:20 PM

colin said:

My images are broken when i mouseover them is there any code missing that i should put in the front end?? Also in order not to get an error on my page node["AltImg"] != null in first if statement

# March 19, 2008 10:16 AM

Russ Morlando said:

Hope this isn't a dead thread.  A little tweaking during conversion to VB.net and it worked like a charm.  Thanks for a simple solution.

# July 11, 2008 10:29 PM

ash143gupta said:

Hi i have applied and tested your code but everything is working fine but my text is not coming from my sitemap file. Although it is navigating but still the text is not displaying and if it does not than all this hardwork is a waste because than i could use simple links and images to achieve this do we have a workaround or i am missing something, i am a vb.net developer and just translated your code to vb.net and added one or two new things but text is not displaying.

Please help

# July 13, 2008 5:07 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)