我正在尝试使用 cookie/会话制作一个简单的购物车。根据此代码片段,它仅包含 4 个项目,
<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>
我正在尝试的是选择所有四个项目,然后单击按钮,它将带我进入下一页以输入价格并计算价格,在另一页中我将显示含税总额。我被困在第二页,因为它只显示第一个选择而不是其他三个。下面是第二页的代码:
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"]);
}
}
}
大家有什么想法我可能错了吗?
将用户添加到购物车的所有商品存储在 Cookie 上是一个坏主意,因为 Cookie 对可以存储的数据数量有限制,其次页面在每个请求上都会携带所有这些数据,这会使页面难以加载。
如果您尝试将其保存在会话中,那么当会话过期时,用户可能会丢失购物车中的商品,甚至在结帐时也会发生这种情况。例如,用户开始结帐,停止几分钟去做某事,然后当其尝试继续时会话已过期。
正确的方法是将购物车保存在数据库中,并与至少有六个月有效期的用户 cookie 连接。
关于你的代码
您的代码中的错误是您对所有cookie使用相同的名称,这就是为什么您只看到第一个。看,这都是一样的,线路上没有变化。
Request.Cookies["UserInfo"]["items"]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)