alternate image in imagefield

Posted on 2007-10-11
Last Modified: 2012-06-27
Hi experty type ppl,

If an image doesn't exist I'd like to replace it with a default image. The following is the code I have.

<asp:ImageField DataImageUrlFormatString="~/PhotoDisplay.ashx?photoid={0}&amp;size=small"
DataImageUrlField="PreviewImageId" SortExpression="PreviewImageId" AlternateText="No Photo.">
      <ItemStyle CssClass="col_photo" />
      <HeaderStyle CssClass="col_photo" />
Question by:Netlink2
    LVL 16

    Expert Comment

    The subsititution of the defualt image would best be done in your handler.

    Purists would also say that Alternate Text should describe the image (for text readers) not be used in the absence of a valid image.
    LVL 16

    Accepted Solution

    I'm assuming that at some point in your Handler you load the image into an in memory representation, if so you could do something like this: -

    string file = "filenameofimagetoload"

    Image oImg = null;

                oImg = Image.FromFile(file);
                oImg = Image.FromFile(HttpContext.Current.Server.MapPath("~/images/ErrorImage.jpg"));
    LVL 1

    Author Comment

    I ended up using codebehind with vb to see if the file exists. Your suggestion pointed me in the right direction.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now