We help IT Professionals succeed at work.

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

cmerlo1
cmerlo1 asked
on
1,712 Views
Last Modified: 2008-01-09
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...
Comment
Watch Question

<%
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%>

Author

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...
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
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.

Author

Commented:
Just tried asvforce's suggestion, got this error:

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

What is the database you are using.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks!  It worked great...
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.