2 u014538058 u014538058 于 2014.04.04 09:11 提问

asp.net的session对象购物车

我刚学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"); }




<br> .style1<br> {<br> text-align: left;<br> }<br> .style2<br> {<br> text-align: center;<br> font-weight: 700;<br> }<br> .style3<br> {<br> font-weight: 700;<br> text-align: center;<br> }<br> .style4<br> {<br> font-weight: 700;<br> text-align: center;<br> }<br> .style5<br> {<br> font-weight: 700;<br> text-align: center;<br> }<br> .style6<br> {<br> font-weight: 700;<br> text-align: center;<br> }<br> .style7<br> {<br> font-weight: 700;<br> text-align: center;<br> }<br> .style8<br> {<br> height: 21px;<br> }<br> .style9<br> {}<br> #form1<br> {<br> text-align: left;<br> }<br>



</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


ImageUrl="~/图片/爱德华的奇妙之旅.jpg" Width="234px" />
     
ImageUrl="~/图片/小时候就想做的事.jpg" Width="234px" />
  
ImageUrl="~/图片/天蓝色的彼岸.jpg" Width="234px" />




Width="234px">/asp:Label
       
Width="234px">/asp:Label
  
Width="234px">/asp:Label




       

  /asp:Label

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<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"]); }




<br> .style1<br> {<br> height: 111px;<br> }<br> #form1<br> {<br> height: 65px;<br> width: 932px;<br> }<br>



</div>
<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server">去收银台</asp:LinkButton>

  
继续购物/asp:LinkButton



要怎么做呢?

1个回答

askjhx
askjhx   2014.04.04 11:30

你可以点击购买这个按钮以后创建一个
HashTable shopping = new HashTable();
点击购买后往shopping.add("商品ID",商品对象);
每次点击都往这个shopping对象中添加一条记录 ,添加完以后把Session["Shop"] = shopping ;展示就直接把Sessiong["Shop"]对象中的Table强转一下就好了 !

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!