2012-05-06 20 views
0

你好,我正在做一個學校的項目和我被困在一個問題一個文本框獲得新價值的任何幫助將是巨大不能從側面一個GridView

我有一個購物車的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(與RowCreatedRowDataBound不同)。

+0

非常感謝:) – user996502

+0

我有同樣的問題,沒有得到在文本框中的新值,這是由於在頁面加載重新綁定GridView。 –