0

我正在使用 ASP.NET 设计一个购物车应用程序。当用户完成购物时,将显示总应付金额。然后他关闭了浏览器。Next time when the browser is opened the quantity of the items still remains unchanged and when the an item is selected the old quantity is displayed.

可能是因为 ASP.NET Development Server 处于运行状态,并显示在通知区域中。

我正在使用的 Check-Out 按钮的代码如下。

    protected void btnCheckout_Click(object sender, EventArgs e)
    {
        List<CD> CdCheckOutList = new List<CD>();
            CdCheckOutList = (List<CD>)Session["cart"];
            double totalPrice = 0.0;

            foreach (var cd in CdCheckOutList)
            {
                totalPrice += cd.Amount;
            }
            lblTotal.Text = "Total Price = "+totalPrice.ToString();

    }

请帮我解决这个问题。

4

2 回答 2

0

onunload 事件的调用将解决您的问题。看看下面的一段代码,它会对你有所帮助。

代码背后:

public static void ClearSession()
{ 
    Session["cart"] = null;
}

Jquery 和 Ajax 调用服务器端调用:

$(window).unload(function() {

 $.ajax({
    type: "POST",
    url: "PageName.aspx/ClearSession",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
         // Do something interesting here.
    }
});
于 2013-07-21T12:31:09.533 回答
0

在结帐过程结束时,您有两个选择:

仅从 中删除cart项目Session,如下所示:

Session.Remove("cart");

cart将键保留在中Session,但将值设为 null,如下所示:

Session["cart"] = null;

至于当用户单击浏览器上的关闭按钮时,我会参考上一个 StackOverflow 问题,用于javascript 检测浏览器关闭选项卡/关闭浏览器

于 2013-07-21T11:28:39.760 回答