problem with image swapping on imagebutton

J

Jeff

hi

asp.net 3.5

<asp:ImageButton ID="ImageButton1" ImageUrl="~/Images/normal.gif"
runat="server" />

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ImageButton1.Attributes.Add("onMouseOver",
"ImageUrl='~/Images/hover.gif'");
ImageButton1.Attributes.Add("onMouseOut",
"ImageUrl='~/Images/normal.gif'");
}
}

when the page loads it displays the ImageButton with the correct image, but
when I hover the mouse over the image then I get a broken image icon. when I
move the mouse away from the ImageButton I still get the broken image
icon...

any ideas what I do wrong here?
 
W

William Niver

Try:

ImageButton1.Attributes.Add("onMouseOver",
HttpContext.Current.Request.ApplicationPath.ToString + "/Images/hover.gif'");
ImageButton1.Attributes.Add("onMouseOut",
HttpContext.Current.Request.ApplicationPath.ToString + "/Images/normal.gif'");
 
W

William Niver

What I posted here would be incorrect.
It would be:

ImageButton1.Attributes.Add("onMouseOver",
this.src='" + HttpContext.Current.Request.ApplicationPath.ToString +
"/Images/hover.gif'");

You are adding Javascript event handlers as attributes to the final img tag
that's being produced in HTML. You want the onmouseover and onmouseout
events to change teh img tags src attribute. I didn't add the
"this.src="[file path]" in my previous post, my appologies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top