简体   繁体   English

C#.Net中的三元运算符和Eval-无法将字符串转换为bool

[英]Ternary Operators and Eval in C# .Net - Cannot convert string to bool

I have a project that has data that may or may not have files associated with it. 我有一个项目,该项目的数据可能与该文件关联,也可能没有。 If the record does not have a file associated with it, I do not want to display the link/label. 如果记录没有与之关联的文件,则我不想显示链接/标签。 I thought this would be fairly simple to implement with the ternary operator, but I receive the following: 我认为使用三元运算符实现起来相当简单,但是我收到以下信息:

: CS0030: Cannot convert type 'string' to 'bool'

resulting from the line containing this statement 由包含此语句的行产生

<asp:Label Visible='<%#  Eval("supportingDocuments") == null ? "False" : "True" %>' id="supportingFileLink" runat="server" Text='Other' /> 

To me, this implies that it is seeing Eval("supportingDocuments") == null as a string and not a conditional that can be evaluated as a boolean. 对我而言,这意味着它将Eval("supportingDocuments") == null视为字符串,而不是可以评估为布尔值的条件。 I have tried adding parentheses around the condition, but this did not change anything. 我尝试在条件周围加上括号,但这并没有改变任何内容。

EDIT: This is an a databound GridView control; 编辑:这是一个数据绑定的GridView控件; sorry for not mentioning that earlier. 抱歉,您之前没有提到。

It must not be liking the double quotes around "True" and "False" . 一定不能喜欢"True""False"周围的双引号。

You could try: 您可以尝试:

Visible='<%#  Eval("supportingDocuments") == null ? false : true %>'

Or better still, you can probably simplify to: 或者更好的是,您可以简化为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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