我正在尝试将项目添加到存储在会话中的购物车,但是当我将新产品添加到购物车时会话值被覆盖。
private List<int> newCart = new List<int>();
protected void dlstCartItems_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
var arg = e.CommandArgument;
DropDownList ddlList = e.Item.FindControl("ddlAvailableSizes"+e.CommandArgument) as DropDownList;
int currentItemID = int.Parse(this.dlstCartItems.DataKeys[e.Item.ItemIndex].ToString());
if (ddlList.SelectedIndex == 0)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Please Select Size');", true);
}
else
{
newCart.Add(Convert.ToInt32(e.CommandArgument));
Session["Cart"] = newCart;
if (Session["Cart"] != null)
{
int ct = ((List<int>)Session["Cart"]).Count;
lblCartMessage.Text = Convert.ToString(ct)+" "+"Product";
}
}
}
}
我的问题是,当我添加新产品时,旧产品不再出现在列表中,并且产品计数始终为 1。