Hi there, There are two simple ways come to mind: + Use client side script to detect a bronken link. As you already know that the image control is rendered as an img element at the client side. If the url specified in the src property is a broken link (missing image), the onerror event will be raised. So you basically can register an event handler for this event to set the default image in this case. The sample code in your web page is something like this:
function Image_OnError(obj)
{
obj.src = "Images/defaultPhoto.jpg";
}
...
<asp:Image onerror="Image_OnError(this);" id="Image1"
runat="server" ImageUrl="Images\myphoto.jpg"></asp:Image>
+ Use server side code to detect a broken link. You simply create a seperate web page which is resposible for detecting/loading the requested image, say ShowImage.aspx. In the Page_Load event, you just assign the ShowImage.aspx with the image url to the ImageUrl property of the image control:
private void Page_Load(object sender, System.EventArgs e)
{
string url = "http://localhost/WebApplication1/Images/MyPhoto.jpg";
url = Server.UrlEncode(url);
Image1.ImageUrl = "ShowImage.aspx?url=" + url;
...
}
The sample code for the ShowImage.aspx goes like this:
private void Page_Load(object sender, System.EventArgs e)
{
string url = Request.QueryString["url"];
url = Server.UrlDecode(url);
if(ImageExits(url)
{
LoadRequestedImage(url);
}
else
{
LoadDefaultImage();
}
}
Perhaps, you may want to use the first option because of the performance as you are expecting.