String was not recognized as a valid Boolean when added to visible attribute

梦想与她 提交于 2019-12-07 04:11:56

问题


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.


回答1:


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;


来源:https://stackoverflow.com/questions/24805011/string-was-not-recognized-as-a-valid-boolean-when-added-to-visible-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!