私は電子商取引アプリに取り組んでいますが、問題は購入した商品の数量を表示することです。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();
}
}
}
}
分かりやすいように画像も貼っておきます。