Monday, September 13, 2010

Showing default image when image not found

                      
 Problem:

         Recently we have found one problem in our  application when  image is not available the   firefox not showing any thing .In IE just showing Red cross mark Which is not good from client prospective  .
           
                                 So What I want to do is have a "default" image that is displayed to anyone who requests a image that is no longer in a specific directory. (basically any 404 request in a specific directory).

Solution:
                                Don't write any server side   logic for  reading external web server image  file .You may or may not have permission to read .Just use java script "onerror " event (I have given sample code below )It will solve your problem.

                   
                   
Note:
              It will work for any web server .No need to bother about the external web server permission and IpAddress .Let me know if you need any help or clarification .


Example:


 <asp:Image ID="imgHeaderLogo" runat="server" onerror="this.src='http://www.google.com.sg/images/default.png';" />



Explanation:

 In this.src you can specify required dimensional default image based on your requirement .