How to add onerror event to image url of hyperlink?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Colin Rodrigues
    New Member
    • Dec 2010
    • 16

    How to add onerror event to image url of hyperlink?

    I am using an image in a hyperlink...for this image i refer to a url...
    Now my question is, that if this url does not return me a image i want to set a default image from another url...

    thanks in advance!!
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    #2
    what do you mean by "image in a hyperlink"? a hyperlink is traditionally a <a href> object. These objects do not have image properties (they're just text). Do you mean you have an image object that you want to be able to catch if the image is broken or not?
    please explain.

    Comment

    • Colin Rodrigues
      New Member
      • Dec 2010
      • 16

      #3
      In asp.net a hyperlink does have a imageurl property...goog le it...let me explain with the sample code....
      1st I'll show u how i achieved this thru html anchor tag...
      Code:
      <a href="page1.aspx"
      <img alt="" src="http://content.website.com/Events/59x88/image001.jpg" onerror="this.src='http://content.website.com/events/59x88/NoImage.jpg'" />
      </a>
      Now i want to do the same thing with a hyperlink..i need a replacement for the onerror method....
      Last edited by Frinavale; Feb 16 '11, 02:05 PM. Reason: Added code tags.

      Comment

      • yarbrough40
        Contributor
        • Jun 2009
        • 320

        #4
        ahhhh I see. your problem is that you need to place your image object as a separate control within your hyperlink (<a> object) and not as a property:
        Code:
         <a href = "page1.aspx"><img alt="" src='http://content.website.com/Events/59x88/image001.jpg' id="hrefID" onerror="this.src='http://content.website.com/events/59x88/NoImage.jpg';" /></a>
        visual studio intellisence is going to tell you that "onerror" is not a valid attribute of "img" but just ignore it - beacuse as we all know.... yes it is.

        ROCK!

        Comment

        • Colin Rodrigues
          New Member
          • Dec 2010
          • 16

          #5
          You still didnt undstnd me properly...i already wrote da same code above...i have already done that...bt now i want to do it in code behind..by making a object of hyperlink and using two images inside that...

          Comment

          • yarbrough40
            Contributor
            • Jun 2009
            • 320

            #6
            I am trying to assist you but it looks as though you are not being clear. Nowhere did you mention anything about doing this in code-behind. Also, the original code you wrote does not work - it is formatted incorrectly which is why I corrected it for you. Your original goal was "..i need a replacement for the onerror method" and that is all you said.

            Please describe in better detail what you are trying to do and I would be happy to help. here is an example:
            1. there is a button on a form
            2. client clicks this form which posts back the page
            3. in the button click event I want to instantiate a new hyperlink that appears within a placeholder
            4. if (some variable exists) then change the src property of the hyperlink.

            etc...

            Comment

            Working...