0

I am using handler to display image from database in my web page.

I have given url of handler with passing category code for its image. But if no image found from db then I want to display no image in place of that handler url.

My code: I am using below image in my repeater control and want no image if no image found from db.

<img src="Webservice/AppServices.ashx?type=categoryimage&catid=<%# Eval("ProductCategorieId") %>" height="100" width="120" class="catimage" />

How can I check with that url and display no image?

2
  • 1
    Can you post your code to clarify the question? Commented Dec 17, 2012 at 15:23
  • I added sample code for image display in repeater. Commented Dec 17, 2012 at 15:25

1 Answer 1

2

Use onerror event like this:

<img src="MyHandler.ashx?id=myId" onerror="this.src='errorImage.png';" />

this.src should point to an existing image!

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, It worked. Can you please elaborate about onerror behavior. You mean it will use noimage url if no image in url found in srs tag.
This is correct. Just don't write anything inside your handler when db image not exists.
This approach (instead of handling it in your ashx) also has the benefit of being more resilient to backend failures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.