0

私は電子商取引アプリに取り組んでいますが、問題は購入した商品の数量を表示することです。DropDownList は、数量を変更するオプションを示しています。これはグリッドビューにあり、selectedindexchanged イベントがあります。イベント コードは正常に機能しますが、2 つ (またはそれ以上) の製品があり、1 つの行の数量を変更してから他の行の数量を変更すると、前の行の数量がデフォルトの「1」に戻され、合計価格列が表示されます。 DropDownList に設定した価格と数量の乗算。

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

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

        }
    }

分かりやすいように画像も貼っておきます。

ここに画像の説明を入力

4

1 に答える 1

0

あなたのグリッドはドロップダウンリストによって作成された各ポストバックにバインドされていると思うので、1 つの値を変更すると、以前に VS に保存された値が上書きされます。これらの値をリストとして保存し、毎回置き換えるのではなく、変更を追加してください。

とにかく、AutoPostBack プロパティを false に設定し、クライアント側で行計算を更新してから、送信時にサーバー側で検証します。

于 2013-07-11T13:37:41.870 に答える