Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 642
Mr A Avatar asked Apr 13 '11 12:04

Mr A


2 Answers

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")) %>' />
like image 169
Muhammad Akhtar Avatar answered Nov 14 '22 17:11

Muhammad Akhtar


Add the Visible attribute to your Image tag:

   Visible="<%# Eval("newsImage") != null %>"

Although in such cases it might be better to use the ItemDataBound event, it's very easy to use.

like image 36
yellowblood Avatar answered Nov 14 '22 16:11

yellowblood