将字符串添加到visible属性后,无法将其识别为有效的布尔值 [英] String was not recognized as a valid Boolean when added to visible attribute

查看:210
本文介绍了将字符串添加到visible属性后,无法将其识别为有效的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向我的listview itemtemplate表中添加true或false的Visible属性.我所做的是,我在页面加载时设置了一个hiddenfield,以便可以使特定的列可见或不可见.这是我的隐藏字段和列:

隐藏字段

<asp:HiddenField ID="uoHiddenFieldPriority" runat="server" Value="false" />

Td列

<td class="leftAligned" visible='<%# (Convert.ToBoolean(uoHiddenFieldPriority.Value)) %>' >
some Text
</td>

这是我在后端的代码:

  int visibility = 0;
  if (visibility = 0)//sample condition I am using to test if the value is changing
     {
        SelectTH.Visible = false;// this is working, this is for the column header
        uoHiddenFieldPriority.Value = "False"; //this is not
                }

发生的情况是引发了错误字符串未被识别为有效的布尔值".我对C#并不是很熟练,这就是为什么我决定使用这种方式来获取列的可见性.

解决方案

您正在将字符串值"False"分配给Boolean属性,因此在分配它之前,应使用Convert.ToBoolean()方法正确地对其进行转换.

OR

您可以直接分配布尔值false而不用任何引号.

替换为:

uoHiddenFieldPriority.Value = "False"; 

与此:

uoHiddenFieldPriority.Value = Convert.ToBoolean("False"); 

OR

uoHiddenFieldPriority.Value = false;

I'm trying to add a true or false Visible attribute to my listview itemtemplate table. What I did is that I have a hiddenfield that is set at page load so that I can make a specific column visible or not. This is my hiddenfield and column:

Hidden Field

<asp:HiddenField ID="uoHiddenFieldPriority" runat="server" Value="false" />

Td column

<td class="leftAligned" visible='<%# (Convert.ToBoolean(uoHiddenFieldPriority.Value)) %>' >
some Text
</td>

This is my code in the backend:

  int visibility = 0;
  if (visibility = 0)//sample condition I am using to test if the value is changing
     {
        SelectTH.Visible = false;// this is working, this is for the column header
        uoHiddenFieldPriority.Value = "False"; //this is not
                }

What happens is that the error "String was not recognized as a valid Boolean" is thrown. I am not really that proficient with c# which is why I decided to use this way of getting the visibility of a column.

解决方案

You are assigning the String value "False" to the Boolean property so before assigning it ,you should convert it properly using Convert.ToBoolean() method.

OR

You can assign Boolean value false directly without having any quotation marks.

Replace This:

uoHiddenFieldPriority.Value = "False"; 

With This:

uoHiddenFieldPriority.Value = Convert.ToBoolean("False"); 

OR

uoHiddenFieldPriority.Value = false;

这篇关于将字符串添加到visible属性后,无法将其识别为有效的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆