Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


I have a datalist in that I have a Image control and in Image Control I am fetching data from database but thing is this if there is no value in image field in table then I want to show a default image in Image control .

I did something like below:

<asp:Image ID="Image1" Width="75%" Height="75%" runat="server" ImageUrl='<%# Eval("Photo") %>'  />


Please help me.

Thanks in Advance.

Regards,
Mohd Wasif
Posted
Updated 23-Jun-11 20:09pm
v3

You could try something like this:

<asp:Image ID="Image1" Width="75%" Height="75%" runat="server" ImageUrl='<%# (Eval("Photo") ?? "path/to/default/image.jpg") %>'  />


Hope it helps.
 
Share this answer
 
Comments
Mohd Wasif 23-Jun-11 9:48am    
I have given path of default Image but not working .
Mohd Wasif 23-Jun-11 9:49am    
path/to/default/image.jpg" this is a static path or I have to create another column for default image in table
jim lahey 23-Jun-11 11:33am    
it's a static path. have a look at the source in your browser and see if the static path is being rendered correctly.
You can use it as
<asp:Image ID="Image1" Width="75%"  Height="75%" runat="server"   ImageUrl='<%# SetPhoto(Eval("Photo"))  %>'  />


C#
public string SetPhoto(string actualUrl)
{
    if(actualUrl != "")
         return actualUrl;
    else
        return @"~/Images/defaul.png";
}
 
Share this answer
 
v2
hi,
find a default image you want to show, place it under your web site roor, lets say your default image name is default.jpg, now use this code below in your page
<asp:Image ID="Image1" Width="75%" Height="75%" runat="server" ImageUrl='<%# (String.IsNullOrEmpty(Eval("Photo").ToString()) ? Eval("Photo"):"~/default.jpg") %>'  />


Thats it..
 
Share this answer
 
You can modify your query and result null column value to default image.

Select isnull(imageColumn, 'default.jpg') from ImageTable
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900