Search code examples
c#imagebindingdatalist

How to show default/alternate image in datalist binded to image control


Visual studio 2008, Framework 3.5, C#

I have a Datalist control.The Control is binded to Image Control.I am Fetching the Imageurl from database.The database has path of Image like this ~/images/Oneslow008.jpg . but if the oneslow008.jpg is not Available in Images folder, at the moment it is showing placeholder. Instead, I want to show some default image in case the image is missing.

This is my Binded Code asp:Image ID="Image1" runat="server" Height="80px" ImageUrl='<%# Eval("imageUrl1") %>' Width="80px"/>


Solution

  • You could create a style with a background image like this:

    .DefaultImage {
        background-position: center center;
        background-image: url('Images/COMPUTER1.png');
        background-repeat: no-repeat;
    }
    

    Make it small enough so when a database image is loaded, it covers up the background image. Apply the class to the element that displays the image.