2016-08-27 3 views
1

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

答えて

2

のCheckBoxがIsCheckedという名前のブール型プロパティを持って、あなただけの

product.Visible = visible.IsChecked; 
そのプロパティを使用して、まったくboolanに戻って、文字列への変換を必要としません。

ちなみに、あなたのエラーは、チェックボックスのTextプロパティを変換するという事実に起因しています。これはおそらく空の文字列です。ブール値に変換できなかったもの

関連する問題