jmasterx
jmasterx

Reputation: 54103

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.

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

Upvotes: 2

Views: 7515

Answers (2)

Alex
Alex

Reputation: 23300

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

// 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*/

Upvotes: 1

Sachin
Sachin

Reputation: 40970

You are trying to directly convert "true" or "false" ie. string to bool value. So try true/ false instead of string like this.

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

Upvotes: 4

Related Questions