"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Sunday, December 5, 2010

default image when their is no image

In my previous post i told display an image when gridview has no data. Here i'm showing when image control have no image it will show a default image.The same solution can be applied to both ASP.NET Image Controls as well as HTML image controls


ASP.NET Image Control

<asp:Image ID="imgProd" runat="server"
 onerror="this.onload = null;
 this.src='ImageurlORAnyImageHere.jpg';"/>


HTML Image Control

<img id="imgProd1" src="someimage.gif"
 onerror="this.onerror=null;
 this.src='ImageurlORAnyImageHere.jpg';" />


We are using the onerror event of the image tag, which triggers if there is an error while loading an image. If an error occurs, we display 'ImageurlORAnyImageHere.jpg' as the default image. You can even add an image url.

Have a nice day... 'N happy Coding :)

No comments: