0

您好,我正在做一个学校项目,但我遇到了一个问题,任何帮助都会很棒

我有一个购物车网格视图,它有一个文本框数量,用户可以在其中更改数量值,因为某种原因它没有获得新值,只是旧值谢谢!

<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

1 回答 1

4

我假设您也在回发时对 GridView 进行数据绑定。只有在以下情况下您才应该这样做!Page.IsPostBack

if(!IsPostBack) BindGrid();

除此之外,如果您使用 a TemplateField,您应该使用FindControl来获取对您的控件的引用:

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

旁注:您不需要DataControlRowType.DataRow在迭代 GridView 行时检查(与 inRowCreated或不同RowDataBound)。

于 2012-05-06T20:32:34.867 回答