• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1615
  • Last Modified:

ASP.net Image Button- If databound image URL is empty, display image from another URL

I have a datagrid with a template field containing an image button:

<asp:ImageButton ID="ImageButton6" runat="server" CommandName="Select" ImageUrl='<%# Eval("PicURL1") %>' ImageAlign="AbsMiddle" BorderColor="Black" BorderStyle="Double" BorderWidth="3px" Width="50px" />

As you can see, the image URL is bound to a field in my database.  I would like to display an image (images/empty.jpg) if the database field does not contain a valid URL, so that I get that image insted of the empty box with the red X...how do I do this?  I tried adding a 'nullimageurl' to the image button, but that doesn't work...
0
cmerlo1
Asked:
cmerlo1
1 Solution
 
Ashish PatelCommented:
<%
IF IsNull(Eval("PicURL1")) Then
%>
<asp:ImageButton ID="ImageButton6" runat="server" CommandName="Select" ImageUrl='images/empty.jpg' ImageAlign="AbsMiddle" BorderColor="Black" BorderStyle="Double" BorderWidth="3px" Width="50px" />
<%Else%>
<asp:ImageButton ID="ImageButton6" runat="server" CommandName="Select" ImageUrl='<%# Eval("PicURL1") %>' ImageAlign="AbsMiddle" BorderColor="Black" BorderStyle="Double" BorderWidth="3px" Width="50px" />
<%End If%>
0
 
cmerlo1Author Commented:
Just tried it- ran into two different errors.  The first is with 'IsNull'- it errors out and says it isn't declared.  The second issue is with the ImageButton 6 in the else statement- it thinks it's a duplicate control and asks me to rename it...
0
 
the_bachelorCommented:
Or you could use a dataset/table/view as the dasource of your grid and have a SQL Statement
such as SELECT ISNULL(PicURL1, "empty_img_path") populate the datasource
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ashish PatelCommented:
Okay do one thing, the query where you are fetching the values from in there, just do this where use have the PicURL1 column specified. Replace PicURL1 with IsNull(PicURL1, 'images/empty.jpg') As PicURL1 and that should work good.
0
 
cmerlo1Author Commented:
Just tried asvforce's suggestion, got this error:

Wrong number of arguments used with function in query expression 'IsNull(PicURL1, 'images/empty.jpg')'.

0
 
Ashish PatelCommented:
What is the database you are using.
0
 
sujithcnairCommented:
from db
select isnull(picurl,'images/empty.jpg') from table

from code

add afunction in codebehind

 Public Function img(ByVal imgname) As String
        Dim imgstr As String
        If IsDBNull(imgname) Then
            imgstr = "path to empty.jpg"
       else
           imgstr =   imgname
        End If      
        Return imgstr
    End Function

in the datagrid call '<%#img( Eval("PicURL1")) %>'

I personally prefer the first method
0
 
cmerlo1Author Commented:
Thanks!  It worked great...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now