asp.net基於HashTable實現購物車的方法_實用技巧

來源:互聯網
上載者:User

本文執行個體講述了asp.net基於HashTable實現購物車的方法。分享給大家供大家參考,具體如下:

//使用者購買商品時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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.