10

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.

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
Mr A
  • 6,448
  • 25
  • 83
  • 137

2 Answers2

17

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")) %>' />
Olaf
  • 10,049
  • 8
  • 38
  • 54
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • the problem is i am saving Null on the database , if there is no image , therefore it returns a string null, so i want to check if the image string is null then visible=fale else show image – Mr A Apr 13 '11 at 13:11
  • Edited the condition. plz check now. – Muhammad Akhtar Apr 13 '11 at 13:15
  • i tried with the above code, now it doesnt show any images, even when there is a image for that news id – Mr A Apr 13 '11 at 13:29
  • Sorry the condition is != rather ==, I have edited answer. just change the condition and it will work. – Muhammad Akhtar Apr 13 '11 at 13:33
  • Eval("newsImage").ToString() !="Null" .. tried using this it works but still its showing an empty img block for the news which have no image i dont want that – Mr A Apr 13 '11 at 13:34
  • can you check your String , it is comparison string will case senstive. e.g NULL or null or Null, compare to particular value. – Muhammad Akhtar Apr 13 '11 at 14:00
  • i dunno y it is saving in d database like : 'NULL' – Mr A Apr 13 '11 at 14:49
1

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.

yellowblood
  • 1,604
  • 2
  • 17
  • 32
  • the problem is i am saving Null on the database , if there is no image , therefore it returns a string null, so i want to check if the image string is null then visible=fale else show image – Mr A Apr 13 '11 at 13:12
  • but this is exactly what you want. If `Eval("newsImage")` is null, then the Visible attribute will become "false" and you won't see the image. If it's not null, Visible will become "true" and you will see the image. – yellowblood Apr 13 '11 at 13:24
  • Eval("newsImage").ToString() =="Null" then visible= 'false' else visible="true" – Mr A Apr 13 '11 at 13:32
  • Eval("newsImage").ToString() !="Null" .. tried using this it works but still its showing an empty img block for the news which have no image i dont want that – Mr A Apr 13 '11 at 13:34
  • When `Visible` is `false`, the control doesn't get to the client. It shouldn't exist in the response at all. Check if you have a div around it or something like that. – yellowblood Apr 13 '11 at 14:07