我刚学asp.net,要做购物车,不用数据库的那种,只是session对象,现在代码写好了,就是我第二次点击购买的时候,我希望购物车里有两次记录要怎么做,就比如同一件商品,我点击两次购买,购物车里有两个订单
代码如下:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
protected void Button1_Click(object sender, EventArgs e) { Session["a"] = Label1.Text; Session["g"] = Label7.Text +1 +"</br>"; Response.Redirect("~/Default2.aspx"); } protected void Button2_Click(object sender, EventArgs e) { Session["b"] = Label2.Text; Session["h"] = Label8.Text +"</br>"; Response.Redirect("~/Default2.aspx"); } protected void Button3_Click(object sender, EventArgs e) { Session["c"] = Label3.Text; Session["i"] = Label9.Text +"</br>"; Response.Redirect("~/Default2.aspx"); } protected void Button4_Click(object sender, EventArgs e) { Session["d"] = Label4.Text; Session["j"] = Label10.Text +"</br>"; Response.Redirect("~/Default2.aspx"); } protected void Button5_Click(object sender, EventArgs e) { Session["e"] = Label5.Text; Session["k"] = Label11.Text +"</br>"; Response.Redirect("~/Default2.aspx"); } protected void Button6_Click(object sender, EventArgs e) { Session["f"] = Label6.Text; Session["l"] = Label12.Text +"</br>"; Response.Redirect("~/Default2.aspx"); }</div>
<asp:Image ID="Image2" runat="server" Height="234px"
ImageUrl="~/图片/时代广场的蟋蟀.jpg" Width="234px" />
ImageUrl="~/图片/查理和巧克力工厂.jpg" Width="234px" />
ImageUrl="~/图片/了不起的狐狸爸爸.jpg" Width="234px" />
Width="243px">/asp:Label
Width="234px">/asp:Label
Width="234px">/asp:Label
CssClass="style8" UseSubmitBehavior="False" />
/asp:Label
UseSubmitBehavior="False" />
/asp:Label
/asp:Label
<asp:Button ID="Button5" runat="server" Text="购买" onclick="Button5_Click" />
/asp:Label
/asp:Label
PostBackUrl="~/Default2.aspx">去购物车/asp:LinkButton
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
protected void Page_Load(object sender, EventArgs e) { Session["Buy"] = Convert.ToInt32(Session["Buy"]); Response.Write(Session["a"]); Response.Write(Session["g"]); Response.Write(Session["b"]); Response.Write(Session["h"]); Response.Write(Session["c"]); Response.Write(Session["i"]); Response.Write(Session["d"]); Response.Write(Session["j"]); Response.Write(Session["e"]); Response.Write(Session["k"]); Response.Write(Session["f"]); Response.Write(Session["l"]); }</div>
<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server">去收银台</asp:LinkButton>
继续购物/asp:LinkButton
要怎么做呢?
你可以点击购买这个按钮以后创建一个
HashTable shopping = new HashTable();
点击购买后往shopping.add("商品ID",商品对象);
每次点击都往这个shopping对象中添加一条记录 ,添加完以后把Session["Shop"] = shopping ;展示就直接把Sessiong["Shop"]对象中的Table强转一下就好了 !