Default image in asp:image control

P

Paul Cheetham

Hi,

I have an application that will display user-defined images in some
asp:image controls.
(sets the ImageURL, and is always built from the site root)

The problem comes when the images specified do not exist, as it displays
a red cross and the alternate text.

What I would like to do is display a default image instead.

Does anyone know of a way to do this?
I have searched all over and the control would appear to be a little
lacking in this respect. There does not appear to be a way of
determining whether or not it has successfully loaded the image, so I
don't know when to display the default.

Unfortunately I cannot use FileExists, because I do not have a physical
path.

Any help appreciated - It's driving me mad!

Thankyou.


Paul
 
G

Guest

Hi Paul,

The problem is related to the fact that browser is responsible for
downloading image from location specified in src attribute (as it seems you
may not know that every asp.net server control renders HTML, Image control
renders NavigateUrl as src attribute of the <img/>. It's very easy to solve
'non-existing' image problem with DOM and javascript. you can handle onerror
event as follows:


<asp:Image runat="server" ID="img" ImageUrl="nonexistingimage.gif"
onerror="DisplayDefaultImage(this)"/>
<script language="javascript">
//<!--
function DisplayDefaultImage(img)
{
img.src = 'DefaultImage.jpg';
}
//-->

Hope this heps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top