服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - asp.net基于HashTable实现购物车的方法

asp.net基于HashTable实现购物车的方法

2019-12-29 13:27happy664618843 ASP.NET教程

这篇文章主要介绍了asp.net基于HashTable实现购物车的方法,涉及asp.net中HashTable结合session实现购物车功能的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了asp.net基于HashTable实现购物车的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//用户购买商品时
if (e.CommandName.ToLower() == "buy")
{
 //判断用户购物车是否为空 如果为空则分配一个
 Hashtable table;
 if (Session["car"] == null)
 {
  table = new Hashtable();
 }
 else
 {
  //用户购物车己存在 则取出数据
  table = Session["car"] as Hashtable;
 }
 //如果用户购物车中不包括该商品信息 则添加一个新商品
 if (!table.Contains(e.CommandArgument))
 {
  table.Add(e.CommandArgument, 1);//添加一个新商品 数量为1
 }
 else
 {
  //如果购物车己存在该商品信息 则将该商品的数量加1 根据HashTable的键获取相对应的值
  int count = Convert.ToInt32(table[e.CommandArgument].ToString());
  //给该商品数量加上1
  table[e.CommandArgument] = (count + 1);
 }
 //保存商品信息
 Session["car"] = table;
 Response.Redirect("shoppingcar.aspx");
}
//商品信息列表
private void shoplist()
{
  Hashtable table;
  if (Session["car"] == null)
  {
   table = new Hashtable();
  }
  else
  {
   table = Session["car"] as Hashtable;
  }
  if (table.Count == 0)
  {
   Image13.Visible = true;
   Msg.Visible = true;
   Msg.Text = "<b style="color:red" mce_style="color:red">您还没有购物呢?赶快购物吧!</b>";
  }
  string[] Arrkey = new string[table.Count];
  int[] ArrVal = new int[table.Count];
  table.Keys.CopyTo(Arrkey, 0);
  table.Values.CopyTo(ArrVal, 0);
  //定义字符串 形成 ('1,2,3')
  string Products = "('";
  int k = 0;
  for (int j = 0; j < Arrkey.Length; j++)
  {
   if(k>0)Products += "','"; k++;
   Products += Arrkey.GetValue(j).ToString();
  }
  Products += "')";
  DataSet ds = productbll.GetInfoByWhere(" pid in " + Products);
  DataTable Table1 = new DataTable();
  Table1 = ds.Tables[0];
  Table1.Columns.Add(new DataColumn("shuliang", System.Type.GetType("System.Int32")));
  //得到pid的值 并将它设置为Table1的主键
  DataColumn[] keys = { Table1.Columns["pid"]};
  Table1.PrimaryKey = keys;
  foreach (string key in table.Keys)
  {
   Table1.Rows.Find(key)["shuliang"] = table[key];//根据键获取值 商品的数量
  }
  Table1.Columns.Add(new DataColumn("zongjia", System.Type.GetType("System.Double"), "hotprice*shuliang"));
  for (int n = 0; n < Table1.Rows.Count; n++)
  {
   tPrice +=Convert.ToDouble(Table1.Rows[n]["zongjia"]);
  }
  Label1.Text = tPrice.ToString();
  Session["total"] = Label1.Text.ToString();
  MyGrid.DataSource = Table1.DefaultView;
  MyGrid.DataBind();
}
#region 从购物车中删除一条商品信息
protected void MyGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
  Hashtable table;
  if (Session["car"] == null)
  {
   table = new Hashtable();
  }
  else
  {
   table = Session["car"] as Hashtable;
  }
  //如果点击删除按钮 则从购物车中移除该商品信息
  if (e.CommandName.ToLower() == "delete")
  {
   if (table.ContainsKey(e.CommandArgument))
   {
    //从HashTable中移除该商品的信息(商品编号) 键:为商品编号 值为:商品数量
    table.Remove(e.CommandArgument);
   }
   Msg.Text = (string)e.CommandArgument;
  }
  Session["car"] = table;
  //调用方法
  shoplist();
}
#endregion

希望本文所述对大家asp.net程序设计有所帮助。

延伸 · 阅读

精彩推荐