Sam
Sam

Reputation: 432

display [no image found] when image src returns no image

I generate my image src from databse field called [pictureName]. I want when Picture name is null or the name doesn't return image to display [image not found] picture.
Here is my aspx code to generate img:

<img src="WEBIMAGES/<%#Eval("BrandName")%>/<%# Eval("PictureName") %>" alt="" title="<%# Eval("ProductName") %>">

my images divided into folders for each brand. i have noImage.jpg in every folder.
how to display this image when no image displayed.
I'm using asp.net webforms
I tried to use onerror event, but not working.

Thanks in advance

Upvotes: 1

Views: 335

Answers (2)

Ondipuli
Ondipuli

Reputation: 468

Replace your code like this

<img src="WEBIMAGES/<%#Eval("BrandName")%>/<%# string.IsNullOrEmpty(Eval("PictureName").ToString()) ? "noImage.jpg" : Eval("PictureName") %>" alt="" title="<%# Eval("ProductName") %>">

Upvotes: 1

hutchonoid
hutchonoid

Reputation: 33306

You could use a coalesce on the picture name as follows:

<img src="WEBIMAGES/<%#Eval("BrandName")%>/<%# Eval("PictureName") ?? 'noImage.jpg' %>" alt="" title="<%# Eval("ProductName") %>">

Upvotes: 3

Related Questions