简体   繁体   中英

why does this NOT work for me? it always returns TRUE

I have been pulling my hair out trying to figure out why this seems to work for others but not me.

I have some data and in the Hours2 column there are clearly cells with NULL and some with hours like this

'9:00 a.m. - 6:00 p.m.'
NULL
NULL
'9:00 a.m. - 6:00 p.m.'
'9:00 a.m. - 5:00 p.m.'
NULL
NULL
NULL

yet I can't figure out how to do what seems like if should be a simple IF statement with some inline code. I already use

<%# Eval("Hours2") %> to get the values and that shows up fine...

IN the first case I want to display one or the other bit of text if Hours2 is NULL or not. (this is the label text for my hours one data, if there is Hours2 then Friday has a different time)

<%# Eval("Hours2") != DBNull.Value ? "mon - thurs" : "mon - fri" %>

This based on answers from other on other things here at Stack sounds like it should work, but it's ALWAYS TRUE...

same thing if i'm trying to just use True or False to make things Visible or not...

Visible ='<%# Eval("Hours2") != DBNull.Value %>'

I'm clearly missing some thing here...!!!

Move the logic to the codehind:

<%# GetString(Eval("Hours2")) %>

Then use your debugger to figure out what is actually being passed.

public string GetString(object hours){
 ...
}

try this:

Visible ='<%# Eval("Hours2").ToString() != "" %>'

or

Visible ='<%# !String.IsNullOrEmpty(Eval("Hours2").ToString()) %>'

您可以尝试以其他方式检查它...这就是我可能要做的。

<%#(String.IsNullOrEmpty(Eval("Hours2").ToString()) ? "mon-thurs" : "mon-fri")%>

how you can get it from SQL. Means using LINQ or call SP? You can use isnull(Hours2,0) Hours2 or isnull(Hours2,'') Hours2 in your Query. and it will work for you like this.

<%# Eval("Hours2") != 0 ? "mon - thurs" : "mon - fri" %>

OR

<%# Eval("Hours2") != '' ? "mon - thurs" : "mon - fri" %>

OR

<%# !String.IsNullOrEmpty(Eval("Hours2").ToString()) ? "mon - thurs" : "mon - fri" %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM