user996502
user996502

Reputation: 307

Can't get new value from a text box in side a gridview

Hello I'm doing a school project and I'm stuck on a problem any help would be great

i have a Shopping Cart Gridview that has a text box quantity where a user can change quantity value for some reason it is not getting the new value just the old value thank you!

<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>

The code behind

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

Upvotes: 0

Views: 2932

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460018

I assume that you're databinding the GridView also on postbacks. You should do that only when !Page.IsPostBack:

if(!IsPostBack) BindGrid();

Apart from that, if you use a TemplateField, you should use FindControl to get a reference to your control:

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

Sidenote: You dont need to check for DataControlRowType.DataRow when iterating the GridView rows(unlike in RowCreated or RowDataBound).

Upvotes: 4

Related Questions