locked
check if image exists RRS feed

  • Question

  • User-1106823036 posted

    I want to display images in my homepage.so I retrieve the path from the database but seems that some of the images doesn't exist in the selected folder.

    I want to display a default image if the image doesn't exist in the folder(default.jpg)

    but how can I check if the image really exist in the folder. I was thinking of checking if the image has content or not but I can't find a sample code for checking image content.

    here is my current code

     <div class="image col-3" runat="server" visible='<%#IIf(Eval("Image") Is DBNull.Value, False,  True) %>'>
                                                        <div class="pad">
                                                            <img src="<%# Eval("Image")%>" />
                                                        </div>
                                                    </div>

    I want instead of Is DBNull.value to check for the content value if null then don't display.

    anyone can help please and thank you

    Friday, January 22, 2016 6:44 AM

Answers

  • User1577371250 posted

    Hi,

    ONERROR add the default image path

    <img src="<%# Eval("Image")%>" onerror="this.src='~/images/defaultimage.jpg'" />
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 22, 2016 6:54 AM

All replies

  • User1577371250 posted

    Hi,

    ONERROR add the default image path

    <img src="<%# Eval("Image")%>" onerror="this.src='~/images/defaultimage.jpg'" />
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 22, 2016 6:54 AM
  • User-1716253493 posted

    Lokesh is right, but maybe will not work with "~/" address

    Try like this

    onerror="this.src='defaultimage.jpg';"
    onerror="this.src='images/defaultimage.jpg';"



    Friday, January 22, 2016 7:02 AM