简体   繁体   English

DropDownList不存储值

[英]DropDownList is not storing values

I am working on an e-commerce app and the problem is in showing the quantity of items purchased. 我正在开发一个电子商务应用程序,问题在于显示购买的商品数量。 The DropDownList shows the option to change quantity, it is in a gridview and it has a selectedindexchanged event. DropDownList显示更改数量的选项,它在网格视图中,并且具有selectedindexchanged事件。 The event code works fine but when there are two(or more) products and we change the quantity of one row and then change the other rows's quantity, the previous row quantity is set back to default that is "1" while total price column shows the multiplication of price and quanity that we set to the DropDownList. 事件代码可以正常工作,但是当有两个(或更多)产品并且我们更改一行的数量然后更改另一行的数量时,前一行的数量将设置为默认值“ 1”,而总价列显示我们为DropDownList设置的价格和数量的乘积。

My DropDownList code: 我的DropDownList代码:

<asp:TemplateField HeaderText="Qty">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlQty" runat="server" AutoPostBack="True" 
                            onselectedindexchanged="ddlQty_SelectedIndexChanged">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                            <asp:ListItem>4</asp:ListItem>
                            <asp:ListItem>5</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>

My C# code for the SelectedIndexChanged and GridView RowDataBoundevent: 我的SelectedIndexChanged和GridView RowDataBoundevent的C#代码:

protected void ddlQty_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlQty =(DropDownList)sender;
        int rowindex = int.Parse(ddlQty.Attributes["rowindex"].ToString());
        ViewState["index"] = rowindex;
        ViewState["I"] = ddlQty.SelectedValue;

        decimal price = Convert.ToDecimal(ds.Tables[0].Rows[rowindex]["Price"].ToString());
        decimal totalprice = price * Convert.ToInt32(ddlQty.SelectedValue);
        ds.Tables[0].Rows[rowindex]["TotalPrice"] = totalprice;
        GridView1.DataSource = ds;
        GridView1.DataBind();
        ds.Tables[1].Rows[0]["TotalPrice"] = decimal.Parse(ds.Tables[1].Rows[0]["TotalPrice"].ToString()) + totalprice - Convert.ToDecimal(ds.Tables[0].Rows[rowindex]["Price"].ToString());
        GridView1.FooterRow.Cells[2].Text = "Total Amount =";
        GridView1.FooterRow.Cells[3].Text = ds.Tables[1].Rows[0]["TotalPrice"].ToString();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddlQty");
            ddl.Attributes.Add("rowindex", e.Row.RowIndex.ToString());
            if (ViewState["index"] != null)
            {
                if (e.Row.RowIndex.ToString() == ViewState["index"].ToString())
                {
                    ddl.SelectedValue = ViewState["I"].ToString();
                }
            }

        }
    }

I am attaching an image also to make it more clear. 我还要附加图像以使其更清晰。

在此处输入图片说明

I think your grid is binding on each postback made by dropdownlist, so changing one value will overwrite the formerly stored value in VS. 我认为您的网格绑定在dropdownlist进行的每个回发上,因此更改一个值将覆盖VS中以前存储的值。 You should try storing these values as a list, appending changes to it instead of replacing it each time. 您应该尝试将这些值存储为列表,将更改附加到列表中,而不是每次都替换它。

IMHO, anyway, I would go by setting AutoPostBack properties to false, and updating row calculations client-side, then validating on server side on submit. 恕我直言,无论如何,我会通过将AutoPostBack属性设置为false,并在客户端更新行计算,然后在提交时在服务器端进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM