用Hashtable表和Session做购物车
1、我用的商品展示页是DataList控件。
2、我做图书购物车,购买按钮用的是button按钮,代码如下;
![]()
1
int bookid = Convert.ToInt32(e.CommandArgument); //得到书的id
2
if (e.CommandName == "shopping") //判断执行DataList中的那个控件的事件
3
{
4![]()
5
if (Session["sp"] == null) //判断Session是不是空
6
{
7
Hashtable ht = new Hashtable(); //创建一个Hashtable表
8
ht.Add(bookid, 1); //因为哈希表是一个键值对表我用他的key来存书的id,Values用来存数量第一次购买时就1
9
Session["sp"] = ht; //把哈希表存入Session中
10
}
11
else
12
{
13
Hashtable ht = (Hashtable)Session["sp"]; //当Session中有值时,将Session转换成Hashtable表
14
if (ht[bookid] == null) //判断是否存在重复
15
{
16
ht[bookid] = 1; //不存在就将其数量设为1
17
}
18
else
19
{
20
ht[bookid] = (int)ht[bookid] + 1; //存在将其数量加1
21
}
22
Session["sp"] = ht; //在将其存回到Session中
23
}
24![]()
25
string sUrl = "Default2.aspx"; //Default2.aspx是购物车查看页
26
Response.Redirect(sUrl);
27
}
28
![]()
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
2![]()
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4![]()
5
<html xmlns="http://www.w3.org/1999/xhtml">
6
<head runat="server">
7
<title>无标题页</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
<table>
13
<asp:DataList ID="DataList1" runat="server">
14
<ItemTemplate>
15
<tr>
16
<td><%# Eval("BookName")%></td>
17
<td><%# Eval("BookPrice")%></td>
18
<td>
19
<asp:TextBox ID="TextBox1" runat="server" Text="<%# Showquantity(Container) %>"></asp:TextBox></td> <!--调用Showquantity方法的到其数量-->
20
</tr>
21
</ItemTemplate>
22
</asp:DataList>
23
</table>
24
</div>
25
</form>
26
</body>
27
</html>
![]()
1
public partial class Default2 : System.Web.UI.Page
2
{
3
int j = 0; //定义一变量做什么用看下面就知道了
4
protected void Page_Load(object sender, EventArgs e)
5
{
6
7
if (!IsPostBack)
8
{
9
if (Session["sp"] == null) //Session传的值是否为空
10
{
11
Response.Redirect("Default.aspx"); //空返回商品购买页
12
}
13
else
14
{
15
Hashtable ht = new Hashtable();
16
ht = (Hashtable)Session["sp"]; //解析Session
17
j = ht.Values.Count - 1; //给j赋值小于Hashtable数量,为什么这么做还是看下面
18
getbook(); //绑定数据库一方法
19
}
20
}
21
}
22![]()
23
private void getbook()
24
{
25
string or = "";
26
string sql = "";
27
Hashtable ht = new Hashtable();
28
ht = (Hashtable)Session["sp"];
29
int[] s = new int[ht.Keys.Count]; //定义一数组
30
ht.Keys.CopyTo(s, 0); //将哈希表主键(Key)复制到数组中去
31
for (int i = 0; i < s.Length; i++) //得到主键,并用其拼凑一条件语句
32
{
33
Response.Write(s[i]);
34
or += " BookID=" + s[i];
35
if (i < ht.Count - 1)
36
{
37
or += " or ";
38
}
39
}
40
sql = "select * from Books where ";
41
sql += or; //得到一sql语句
42
SqlConnection conn = new SqlConnection(“server=.;database=book;uid= ;pwd= ”);
43
SqlCommand cmd = new SqlCommand(Sql, conn);
44
cmd.Connection.Open();
45
DataSet ds = new DataSet();
46
SqlDataAdapter da = new SqlDataAdapter(cmd);
47
da.Fill(ds);
48
cmd.Connection.Close();
49
this.DataList1.DataSource = ds.Tables[0];
50
DataList1.DataBind();
51
}
52![]()
53
protected string Showquantity(Control container) //的到购买商品的数量
54
{
55
Hashtable ht = new Hashtable();
56
ht = (Hashtable)Session["sp"];
57
int[] d = new int[ht.Keys.Count]; //的到values,就数量
58
ht.Values.CopyTo(d, 0);
59
string shuliang = d[j].ToString(); //j就在这用到了,因为此方法在DataList内调用,DataList在的到数据时自己就是要一个循环
60
j--; //为什么用递减我也不太清楚,一开始为以为是他是堆栈方式存储数据,但进行几次实验后发现不是,但的到的数量和你购买的一样,我已经试验过了
61
return shuliang;
62
}
63
}
2、我做图书购物车,购买按钮用的是button按钮,代码如下;
<asp:Button ID="btnBuy" runat="server" Text="购买" CommandArgument='<%# Eval("bookID") %>' CommandName="shopping" />
3、在DataList的ItemCommand事件中写如下代码:
1
int bookid = Convert.ToInt32(e.CommandArgument); //得到书的id2
if (e.CommandName == "shopping") //判断执行DataList中的那个控件的事件3
{4

5
if (Session["sp"] == null) //判断Session是不是空6
{7
Hashtable ht = new Hashtable(); //创建一个Hashtable表8
ht.Add(bookid, 1); //因为哈希表是一个键值对表我用他的key来存书的id,Values用来存数量第一次购买时就19
Session["sp"] = ht; //把哈希表存入Session中 10
}11
else12
{13
Hashtable ht = (Hashtable)Session["sp"]; //当Session中有值时,将Session转换成Hashtable表14
if (ht[bookid] == null) //判断是否存在重复15
{16
ht[bookid] = 1; //不存在就将其数量设为117
}18
else19
{20
ht[bookid] = (int)ht[bookid] + 1; //存在将其数量加121
}22
Session["sp"] = ht; //在将其存回到Session中23
}24

25
string sUrl = "Default2.aspx"; //Default2.aspx是购物车查看页26
Response.Redirect(sUrl);27
}28

4、编写购物车查看Default2.aspx页的代码。我在其页面只绑定书名、价格、数量。
html(Default2.aspx)中的如下:
html(Default2.aspx)中的如下:
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>2

3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4

5
<html xmlns="http://www.w3.org/1999/xhtml">6
<head runat="server">7
<title>无标题页</title>8
</head>9
<body>10
<form id="form1" runat="server">11
<div>12
<table>13
<asp:DataList ID="DataList1" runat="server">14
<ItemTemplate>15
<tr>16
<td><%# Eval("BookName")%></td>17
<td><%# Eval("BookPrice")%></td> 18
<td> 19
<asp:TextBox ID="TextBox1" runat="server" Text="<%# Showquantity(Container) %>"></asp:TextBox></td> <!--调用Showquantity方法的到其数量-->20
</tr>21
</ItemTemplate>22
</asp:DataList>23
</table>24
</div>25
</form>26
</body>27
</html>Default2.aspx.cs中的代码:
1
public partial class Default2 : System.Web.UI.Page2
{3
int j = 0; //定义一变量做什么用看下面就知道了4
protected void Page_Load(object sender, EventArgs e)5
{6
7
if (!IsPostBack)8
{9
if (Session["sp"] == null) //Session传的值是否为空10
{ 11
Response.Redirect("Default.aspx"); //空返回商品购买页12
}13
else14
{15
Hashtable ht = new Hashtable();16
ht = (Hashtable)Session["sp"]; //解析Session17
j = ht.Values.Count - 1; //给j赋值小于Hashtable数量,为什么这么做还是看下面18
getbook(); //绑定数据库一方法19
}20
}21
}22

23
private void getbook()24
{25
string or = "";26
string sql = "";27
Hashtable ht = new Hashtable();28
ht = (Hashtable)Session["sp"];29
int[] s = new int[ht.Keys.Count]; //定义一数组30
ht.Keys.CopyTo(s, 0); //将哈希表主键(Key)复制到数组中去 31
for (int i = 0; i < s.Length; i++) //得到主键,并用其拼凑一条件语句32
{33
Response.Write(s[i]);34
or += " BookID=" + s[i];35
if (i < ht.Count - 1)36
{37
or += " or ";38
}39
}40
sql = "select * from Books where ";41
sql += or; //得到一sql语句42
SqlConnection conn = new SqlConnection(“server=.;database=book;uid= ;pwd= ”);43
SqlCommand cmd = new SqlCommand(Sql, conn);44
cmd.Connection.Open();45
DataSet ds = new DataSet();46
SqlDataAdapter da = new SqlDataAdapter(cmd);47
da.Fill(ds);48
cmd.Connection.Close();49
this.DataList1.DataSource = ds.Tables[0];50
DataList1.DataBind();51
}52

53
protected string Showquantity(Control container) //的到购买商品的数量54
{55
Hashtable ht = new Hashtable();56
ht = (Hashtable)Session["sp"];57
int[] d = new int[ht.Keys.Count]; //的到values,就数量58
ht.Values.CopyTo(d, 0);59
string shuliang = d[j].ToString(); //j就在这用到了,因为此方法在DataList内调用,DataList在的到数据时自己就是要一个循环60
j--; //为什么用递减我也不太清楚,一开始为以为是他是堆栈方式存储数据,但进行几次实验后发现不是,但的到的数量和你购买的一样,我已经试验过了61
return shuliang;62
}63
}



浙公网安备 33010602011771号