购物车使用Cookie [英] Shopping Cart Using Cookies

查看:123
本文介绍了购物车使用Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用cookie /会话一个简单的购物车。它只是包含根据本code段4项,

I am trying to make a simple shopping cart using cookies/sessions. It just contains 4 items according to this code snippet,

<form id="form1" runat="server">
    <div style="height: 296px">



        <asp:ListBox ID="ListBox1" runat="server" Height="164px" Width="107px" 
            SelectionMode="Multiple">
            <asp:ListItem>Tyres</asp:ListItem>
            <asp:ListItem>Battery</asp:ListItem>
            <asp:ListItem>Front Glass</asp:ListItem>
            <asp:ListItem>Vanity Mirrors</asp:ListItem>
        </asp:ListBox>
        <br />
        <br />
        Username:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        EMail: 
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>



        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />



    </div>
    </form>

我试图为我选择所有四个项目并单击按钮,它会带我到下一个页面中输入价格和计算价格,并在其他网页我将展示与税收总量。我停留在第二页上,因为它只是表明第一选择不其他三个。下面是code的第二页:

what I am trying is I am selecting all four items and on clicking the button, it will take me to the next page for entering prices and calculating prices and in the other page I will show the total with taxes. I am stuck on the second page as it just shows the First selection not the other three. Below is the code for second page:

public partial class confirm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["UserInfo"] != null)
        {
           // TextBox1.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["userName"]);
            //TextBox2.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["email"]);
            Label1.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["items"]);
            Label2.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["items"]);
            Label3.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["items"]);
            Label4.Text = Server.HtmlEncode(Request.Cookies["UserInfo"]["items"]);
        }
    }
}

任何想法的家伙,我可能是错的?

Any ideas guys where I might be wrong?

推荐答案

是一个坏主意来存储所有用户添加到购物车中的饼干因为cookie对你能有多少数据存储限制的项目,第二页面进行每次请求所有的数据,你让你的网页难以负荷。

Is a bad idea to store all the items that the user adds to the cart on cookies because the cookie have limitation on how many data you can store, and second the page carry all that data on every request and you make your page difficult to load.

如果您尝试将其保存在会话,那么用户可能会失去什么的车当会话到期,本可以在退房甚至发生。例如,用户开始查出来,就是停止了几分钟做一些事情,然后当它试图继续会话具有到期。

If you try to save it on the session, then the user may lose whats on the cart when the session expire, and this can happens even at check out. For example, the user starts the check out, is stop for some minutes to do something, and then when its try to continue the session has expires.

正确的方法是保持一个数据库上的车,条件是至少有六个月到期的用户cookie连接。

The correct way is to keep the cart on a database, connected with the user cookie that have an expiration of six months at least.

在你的code中的错误是,您都使用相同的cookie名称,为什么你只看到了第一位。看,这是对线相​​同,没有变化。

the bug on your code is that you use the same cookie name for all, that why you see only the first. See, this is the same and not changes on the lines.

Request.Cookies["UserInfo"]["items"]

这篇关于购物车使用Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆