By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,493 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,493 IT Pros & Developers. It's quick & easy.

Default image in asp:image control

P: n/a
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
Jan 27 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Paul,

You can get the physical path out of virtual one with MapPath method

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Paul Cheetham" <PA******@dsl.pipex.comwrote in message
news:OI**************@TK2MSFTNGP03.phx.gbl...
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

Jan 27 '07 #2

P: n/a
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

--
Milosz
"Paul Cheetham" wrote:
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
Jan 27 '07 #3

P: n/a

Thanks to both of you for your help.
Paul

Paul Cheetham wrote:
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
Jan 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.