繁体   English   中英

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

[英]Additional information: String was not recognized as a valid Boolean

我正在编辑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>

执行页面时出现错误。 错误是字符串未被识别为有效的布尔值。

码:

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();
    }

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

 product.Visible = visible.IsChecked; 

顺便说一句,您的错误是由以下事实引起的:您转换了复选框的Text属性,而这可能只是一个空字符串。 无法转换为布尔值的内容

暂无
暂无

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

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