简体   繁体   English

尝试评估布尔值时出错?

[英]Error trying to eval a bool?

I only want to show a certain item template if the object's CRMID which is a string that can be null. 如果对象的CRMID是可以为null的字符串,我只想显示某个项目模板。

If it is null I do not want to show this item template: 如果为空,我不想显示此项目模板:

      <asp:TemplateField HeaderText="">
                <ItemTemplate>
                <a href="#myModal" id='rm_btn' runat="server" role="button" class="close custom-close"  onclick="showModal('#myModal')" onserverclick="rmbtn"
                    visible='<%# (bool)(DataBinder.Eval(Container.DataItem, "CRMID") == null) ? "true" : "false" %>'>
                    ×</a>
                </ItemTemplate>
            </asp:TemplateField>

However I get the following error: 但是我收到以下错误:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0030: Cannot convert type 'string' to 'bool'

Source Error:


Line 136:                <asp:TemplateField HeaderText="">
Line 137:                    <ItemTemplate>
Line 138:                    <a href="#myModal" id='rm_btn' runat="server" role="button" class="close custom-close"  onclick="showModal('#myModal')" onserverclick="rmbtn"
Line 139:                        visible='<%# (bool)(DataBinder.Eval(Container.DataItem, "CRMID") == null) ? "true" : "false" %>'>
Line 140:                        ×</a>

I am not sure what I am doing wrong... 我不确定自己在做什么错...

Thanks 谢谢

You are trying to directly convert "true" or "false" ie. 您正在尝试直接转换"true""false"即。 string to bool value. string到布尔值。 So try true/ false instead of string like this. 因此,请尝试使用true/ false而不是像这样的字符串。

visible='<%# (bool)(DataBinder.Eval(Container.DataItem, "CRMID") == null) ? true : false %>'>

I can suggest several attempts: 我可以建议几种尝试:

Remove the quotes 删除引号

// BEFORE
(bool)(DataBinder.Eval(Container.DataItem, "CRMID") == null) ? "true" : "false"

// AFTER
(bool)(DataBinder.Eval(Container.DataItem, "CRMID") == null) ? true : false

Or bool.Parse it 还是bool.Parse

// BEFORE
(bool)(DataBinder.Eval(Container.DataItem, "CRMID") == null) ? "true" : "false"

// AFTER
bool.Parse((DataBinder.Eval(Container.DataItem, "CRMID") == null) ? "true" : "false")

Or straight out remove the check 或直接取出支票

// BEFORE
(bool)(DataBinder.Eval(Container.DataItem, "CRMID") == null) ? "true" : "false"

// AFTER
(DataBinder.Eval(Container.DataItem, "CRMID") == null) /* this is a bool already*/

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

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