附加信息:字符串未被识别为有效的布尔值

时间:2016-08-27 07:58:11

标签: c# asp.net

我正在编辑gridview。我也添加了复选框。我需要检查用户点击检查按钮的时间,它应该给出真正的价值。如果不是假的。 这是我的网格视图。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" OnRowDeleting="DeleteRecord" 
        EmptyDataText="There are no data records to display." onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
        AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
         CellPadding="4" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White"  />
        <Columns>

            <asp:TemplateField HeaderText="Product Name" SortExpression="ProductName">  
                    <EditItemTemplate>  
                        <asp:TextBox ID="ProductTextBox" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>  
                    </EditItemTemplate>  
                    <ItemTemplate>  
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>  
                    </ItemTemplate>  
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Description" SortExpression="Description">  
                    <EditItemTemplate>  
                        <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>  
                    </EditItemTemplate>  
                    <ItemTemplate>  
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Description") %>'></asp:Label>  
                    </ItemTemplate>  
                </asp:TemplateField>

            <asp:TemplateField HeaderText="Items In Set" SortExpression="ItemsInSet">  
                    <EditItemTemplate>  
                        <asp:TextBox ID="ItemsTextBox" runat="server" Text='<%# Bind("ItemsInSet") %>'></asp:TextBox>  
                    </EditItemTemplate>  
                    <ItemTemplate>  
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("ItemsInSet") %>'></asp:Label>  
                    </ItemTemplate>  
                </asp:TemplateField>

            <asp:TemplateField HeaderText="Unit Price Owner" SortExpression="UnitPriceOwner">  
                    <EditItemTemplate>  
                        <asp:TextBox ID="PriceOwnerTextBox" runat="server" Text='<%# Bind("UnitPriceOwner") %>'></asp:TextBox>  
                    </EditItemTemplate>  
                    <ItemTemplate>  
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("UnitPriceOwner") %>'></asp:Label>  
                    </ItemTemplate>  
                </asp:TemplateField>

            <asp:TemplateField HeaderText="Unit Price Reseller" SortExpression="UnitPriceReseller">  
                    <EditItemTemplate>  
                        <asp:TextBox ID="PriceResellerTextBox" runat="server" Text='<%# Bind("UnitPriceReseller") %>'></asp:TextBox>  
                    </EditItemTemplate>  
                    <ItemTemplate>  
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("UnitPriceReseller") %>'></asp:Label>  
                    </ItemTemplate>  
                </asp:TemplateField>

             <asp:TemplateField HeaderText="Shipping Cost" SortExpression="ShippingCost">  
                    <EditItemTemplate>  
                        <asp:TextBox ID="CostTextBox" runat="server" Text='<%# Bind("ShippingCost") %>'></asp:TextBox>  
                    </EditItemTemplate>  
                    <ItemTemplate>  
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("ShippingCost") %>'></asp:Label>  
                    </ItemTemplate>  
                </asp:TemplateField>

            <asp:TemplateField HeaderText="In Offer" SortExpression="InOffer">  
                    <EditItemTemplate>  
                        <asp:TextBox ID="InOfferTextBox" runat="server" Text='<%# Bind("InOffer") %>'></asp:TextBox>  
                    </EditItemTemplate>  
                    <ItemTemplate>  
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("InOffer") %>'></asp:Label>  
                    </ItemTemplate>  
                </asp:TemplateField>

            <asp:TemplateField HeaderText="Visible" SortExpression="Visible">  
                    <EditItemTemplate>  
                        <asp:CheckBox ID="VisibleCheckBox" runat="server" />
                    </EditItemTemplate>  
                    <ItemTemplate>  
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Visible") %>'></asp:Label>  
                    </ItemTemplate>  
                </asp:TemplateField>

        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
    </asp:GridView>

我在执行页面时遇到错误。错误是String未被识别为有效的布尔值。

代码:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int productId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

        TextBox productName = GridView1.Rows[e.RowIndex].FindControl("ProductTextBox") as TextBox;
        TextBox description = GridView1.Rows[e.RowIndex].FindControl("DescriptionTextBox") as TextBox;
        TextBox itemsInSet = GridView1.Rows[e.RowIndex].FindControl("ItemsTextBox") as TextBox;
        TextBox unitPriceOwner = GridView1.Rows[e.RowIndex].FindControl("PriceOwnerTextBox") as TextBox;
        TextBox unitPriceReseller = GridView1.Rows[e.RowIndex].FindControl("PriceResellerTextBox") as TextBox;
        TextBox shippingCost = GridView1.Rows[e.RowIndex].FindControl("CostTextBox") as TextBox;
        TextBox inOffer = GridView1.Rows[e.RowIndex].FindControl("InOfferTextBox") as TextBox;
        CheckBox visible = GridView1.Rows[e.RowIndex].FindControl("VisibleCheckBox") as CheckBox;

        Product product = new Product();
        product.ProductID = productId;
        product.ProductName = productName.Text;
        product.Description = description.Text;
        product.ItemsInSet = Convert.ToInt32(itemsInSet.Text);
        product.UnitPriceOwner = Convert.ToInt32(unitPriceOwner.Text);
        product.UnitPriceReseller = Convert.ToInt32(unitPriceReseller.Text);
        product.ShippingCost = Convert.ToInt32(shippingCost.Text);
        product.InOffer = Convert.ToBoolean(inOffer.Text);
        product.Visible = Convert.ToBoolean((visible.Text).ToString());  

        ProductBL.UpdateProduct(product);

        GridView1.EditIndex = -1;
        GridView1.DataSource = ProductBL.GetProducts();
        GridView1.DataBind();
    }

1 个答案:

答案 0 :(得分:2)

CheckBox有一个名为IsChecked的布尔属性,您根本不需要转换为字符串然后再返回boolan,只需使用该属性

 product.Visible = visible.IsChecked; 

顺便说一句,你的错误是由你转换复选框的Text属性引起的,这可能只是一个空字符串。无法转换为布尔值的东西