I'm trying to do an Eval()
on a date column and I've just decided that I don't know how to refer to the
? true : false
part of
<%# Eval("Date") ? true : false %>
But that's exactly where I'm having trouble so I hope my wording in the question title is okay.
Having said that, the regular eval without the ? true : false
part works just fine, it pulls the date value from the correct column. The problem is that some of them are blank so before I do anything else I need to check whether or not the date column even has a value and that's where I'm having trouble.
I've tried this:
<asp:TextBox Text='<%# Eval("Date") ? true : false %>'></asp:TextBox>
and this:
<asp:TextBox Text='<%# Convert.ToBoolean(Eval("Date")) ? true : false %>'></asp:TextBox>
and a few varieties of things like this:
<asp:TextBox Text='<%# Eval("Date") ? true : "" %>'></asp:TextBox>
And before you ask, yes I have my
runat="server"
in there (and an ID, a couple classes and some string formatting), I chopped out anything that isn't relevant to the question
I feel like I'm on the right track, but I don't really understand how to use the ? true : false
part yet and I keep running into errors along the lines of "String is not a valid boolean" or "Cannot implicitly convert String to Bool".
What I'm trying to do is check for a value and if it doesn't have one (IsNullOrEmpty), do this, otherwise do something else. I guess I'm not sure how to write "Is there a value, yes or no?" in the middle of an Eval()
. Then I can add a CssClass based on the result.
I appreciate any help anyone can offer.
Thanks,
Jay
It depends on the type of object you are binding to Control. But you can do this
CssClass='<%# Eval("Date") == null ? "ClassA" : "ClassB" %>'
or
CssClass='<%# string.IsNullOrEmpty(Eval("Date").ToString()) ? "ClassA" : "ClassB" %>'
And if you output is a boolean, you usually set a property with it, not text
Visible='<%# Eval("Date") == null ? true : false %>'