2012-05-06 12 views
0

のGridViewから新しい値を取得できません何らかの理由でユーザーが数量値を変更できるテキストボックス量は、新しい値を取得していないだけです。古い値をお寄せください!は、任意のヘルプが</p> <p>は私が持っているショッピングカートGridViewのを持っている素晴らしいことだ側のテキストボックスこんにちは、私は学校のプロジェクトをやっていると私は問題にこだわっている

<div class="container"> 
      <h1>Shopping Cart</h1> 
      <a href="Default.aspx">&lt; Back to Products</a> 
      <br /><br /> 
     <asp:Label ID="Label2" runat="server" ></asp:Label> 
     <asp:Label ID="Label3" runat="server" ></asp:Label> 
      <asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." GridLines="None" Width="100%" CellPadding="5" ShowFooter="true" DataKeyNames="ProductID" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowDeleting="grdCart_RowDeleting" > 
       <HeaderStyle HorizontalAlign="Left" BackColor="#3D7169" ForeColor="#FFFFFF" /> 
       <FooterStyle HorizontalAlign="Right" BackColor="#6C6B66" ForeColor="#FFFFFF" /> 
       <AlternatingRowStyle BackColor="#F8F8F8" /> 
       <Columns> 
        <asp:BoundField DataField="ProductName" HeaderText="Brand" /> 
        <asp:TemplateField HeaderText="Quantity"> 
         <ItemTemplate> 
          <asp:TextBox runat="server" ID="txtQuantity" Columns="5" Text='<%# Eval("Quantity") %>'></asp:TextBox><br />        
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" /> 
        <asp:BoundField DataField="SubTotal" HeaderText="Total" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" /> 
        <asp:CommandField ShowDeleteButton="True" /> 
       </Columns> 
      </asp:GridView> 
      <br /> 
      <asp:Button runat="server" ID="btnUpdateCart" Text="Update Cart" OnClick="btnUpdateCart_Click" /><br /> 
      <asp:Button ID="btnClose2" runat="server" Text="Close" Width="50px" /> 
     </div> 

protected void btnUpdateCart_Click(object sender, EventArgs e) 
    { 
     foreach (GridViewRow row in gvShoppingCart.Rows) 
     { 
      if (row.RowType == DataControlRowType.DataRow) 
      { 
       // We'll use a try catch block in case something other than a number is typed in 
       // If so, we'll just ignore it. 
       try 
       { 
        // Get the productId from the GridView's datakeys 
        int productId = Convert.ToInt32(gvShoppingCart.DataKeys[row.RowIndex].Value); 
        Label2.Text = productId.ToString(); 
        // Find the quantity TextBox and retrieve the value 
        int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text); 

        Label3.Text = quantity.ToString(); 
        ShoppingCart.Cart.Instance.SetItemQuantity(productId, quantity); 
        BindData(); 
       } 
       catch (FormatException) { } 
      } 
     } 

     mpe2.Show(); 
    } 

答えて

4

の背後にあるコード私はあなたがポストバックでも、GridViewのデータバインディングをしていることを前提としています。あなたはときにのみ!Page.IsPostBackことを行う必要があります。それとは別に

if(!IsPostBack) BindGrid(); 

をごTemplateFieldを使用している場合、あなたはあなたのコントロールへの参照を取得するためにFindControlを使用する必要があります。

var txtQuantity = (TextBox)row.FindControl("txtQuantity"); 
int quantity = int.Parse(txtQuantity.Text); 

は追記:あなたがする必要がいけませんGridViewの行を反復するときは、DataControlRowType.DataRowを確認してください(RowCreatedまたはRowDataBoundのように)。

+0

ありがとうございました:) – user996502

+0

私は同じ問題を抱えていましたが、テキストボックスに新しい値が表示されず、GridViewをページの読み込みに再バインドしたためでした。 –

関連する問題