0

I have a table with values

ID     PicURL            FilePath
1      www.a.com/1.png   NULL
2      NULL              ~/Images/2.jpeg

In code behind I have

<asp:Image ID="imageControl" runat="server" Width="100" ImageUrl='<%#Eval("FilePath")%>'></asp:Image>

How do I include the case when FilePath is null and ImageURL has to get value from PicURL?

Thanks Rashmi

2
  • What part of the code is reading the table? Isn't there a SQL query someplace? Commented Jan 29, 2014 at 18:34
  • Is it always the case that one or the other is null? What if both are populated? Assuming FilePath takes precedence? Commented Jan 29, 2014 at 19:23

3 Answers 3

2

If you don't want to change data structure, or query... and want to do it in code front, you can do it like this (though it's cleaner to do in ItemDataBound):

<asp:Image ID="imageControl" runat="server" Width="100" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "FilePath") != null ? Eval("FilePath") : Eval("PicURL")  %>'></asp:Image>
Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you just create a calculated field/property and do the logic there and then bind that property?

1 Comment

Would probably belong in the comments section, rather than in the answers.
0

You could change your SQL Query to return either PicURL or FilePath based on whichever value is not null. You can reverse the order of the field list depending on which field should take precedence.

SELECT Id, Isnull(PicURL, FilePath) as ImageUrl from tblImage

<asp:Image ID="imageControl" runat="server" 
Width="100" ImageUrl='<%#Eval("ImageUrl")%>'></asp:Image>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.