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. Advertising

  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'");

    "Jeff" wrote:

    > 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?
    >
    >
    >
     
    William Niver, Apr 29, 2009
    #2
    1. Advertising

  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" wrote:

    > 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'");
    >
    > "Jeff" wrote:
    >
    > > 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?
    > >
    > >
    > >
     
    William Niver, Apr 29, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RobertH
    Replies:
    3
    Views:
    4,786
    RobertH
    Feb 17, 2004
  2. Guadala Harry

    Swapping Image Url

    Guadala Harry, Aug 13, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    5,663
    Toby Mathews
    Aug 13, 2004
  3. Richard

    image swapping problem

    Richard, Sep 6, 2003, in forum: HTML
    Replies:
    1
    Views:
    359
    Richard
    Sep 6, 2003
  4. Jeff
    Replies:
    2
    Views:
    359
    Jesse Houwing
    Apr 19, 2009
  5. Hubbers
    Replies:
    0
    Views:
    116
    Hubbers
    Jun 1, 2006
Loading...

Share This Page