problem with image swapping on imagebutton

Discussion in 'ASP .Net' started by Jeff, Apr 29, 2009.

  1. Jeff

    Jeff Guest

    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?
     
    Jeff, Apr 29, 2009
    #1
    1. Advertisements

  2. 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'");
     
    William Niver, Apr 29, 2009
    #2
    1. Advertisements

  3. 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.
     
    William Niver, Apr 29, 2009
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.