tags:

views:

58

answers:

3

In a Asp Net data bound control one can use the nice Eval() syntax:

<div><%# Eval("MyProp") %></div>

but it's not possible to combine with a conditional statement is it?:

<% if (Eval("MyProp")!="") { %>
<div><%# Eval("MyProp") %></div>
<%} %>

Would be nice to have that option.

So - my option is to put part of the markup in CodeBehind. I really liked to keep it out of there. Any other possibilities?

+1  A: 

It's because Eval("MyProp") returns object and you cannot compare object to string. You might need to cast it to string (if MyProp is of type string of course).

Darin Dimitrov
Didnt work for me, and googling around I really think it's a dead end.
joeriks
+1  A: 
Michael Kropat
visible="False" would work, but not such an elegant solution, u think?
joeriks
Thinking of it again - that is indeed a nice solution and it will leave out the whole tag from the markup. (My first thought was it would add style="display:none".) Thanks!
joeriks
A: 
<% if ((string)Eval("MyProp") != "")
 { %>
<div>
    <%# Eval("MyProp") %></div>
<%} %>

or

<%# (string)Eval("MyProp") != "" ? string.Format("<div>{0}</div>", Eval("MyProp")) : "" %>
y34h