using if with eval("") in asp.net

Mr A picture Mr A · Apr 13, 2011 · Viewed 45.3k times · Source

I am using repeater to display the news on the news section. In my news section i have got 2 labels(title, Description) and one image field. Below is the code which i am using to populate the repeater:

<asp:Repeater ID="rptNews" runat="server">
<ItemTemplate>
<asp:Image ID="newsImage" runat="server" ImageUrl='<%#String.format("../Images/News/{0}", Eval("newsImage")) %>' />
<asp:Label ID="newsTitle" runat="server" Text='<%#Eval("newsTitle") %>'></asp:Label>
<br />
<asp:Label ID="newsDescription" runat="server" Text='<%#Eval("newsDescription") %>'></asp:Label>
<br />
<div class="clear">&nbsp;</div>
</ItemTemplate>

</asp:Repeater>

I want to use if statement with the , for instance if the Eval("newsImage") is null then i want to disable the image control and just show the title and description of news . Any suggestions on how to acheive this.

Answer

Muhammad Akhtar picture Muhammad Akhtar · Apr 13, 2011

should be like... Visible='<%# Eval("newsImage").ToString() != "Null" %>'

<asp:Image ID="newsImage" runat="server" Visible='<%# Eval("newsImage").ToString() == "Null" %>'  ImageUrl='<%#String.Format("../Images/News/{0}", Eval("newsImage")) %>' />