質問

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

役に立ちましたか?

解決

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>

他のヒント

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

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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top