asp.net基于HashTable實現(xiàn)購物車的方法
更新時間:2015年12月04日 14:46:49 作者:happy664618843
這篇文章主要介紹了asp.net基于HashTable實現(xiàn)購物車的方法,涉及asp.net中HashTable結合session實現(xiàn)購物車功能的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了asp.net基于HashTable實現(xiàn)購物車的方法。分享給大家供大家參考,具體如下:
//用戶購買商品時 if (e.CommandName.ToLower() == "buy") { //判斷用戶購物車是否為空 如果為空則分配一個 Hashtable table; if (Session["car"] == null) { table = new Hashtable(); } else { //用戶購物車己存在 則取出數(shù)據 table = Session["car"] as Hashtable; } //如果用戶購物車中不包括該商品信息 則添加一個新商品 if (!table.Contains(e.CommandArgument)) { table.Add(e.CommandArgument, 1);//添加一個新商品 數(shù)量為1 } else { //如果購物車己存在該商品信息 則將該商品的數(shù)量加1 根據HashTable的鍵獲取相對應的值 int count = Convert.ToInt32(table[e.CommandArgument].ToString()); //給該商品數(shù)量加上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];//根據鍵獲取值 商品的數(shù)量 } 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中移除該商品的信息(商品編號) 鍵:為商品編號 值為:商品數(shù)量 table.Remove(e.CommandArgument); } Msg.Text = (string)e.CommandArgument; } Session["car"] = table; //調用方法 shoplist(); } #endregion
希望本文所述對大家asp.net程序設計有所幫助。
相關文章
.Net Core2.1 WebAPI新增Swagger插件詳解
這篇文章主要給大家介紹了關于.Net Core2.1 WebAPI新增Swagger插件的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07完美兼容ie和firefox的asp.net網站加入收藏和設置主頁
這篇文章主要介紹了完美兼容ie和firefox的asp.net網站加入收藏和設置主頁,需要的朋友可以參考下2014-12-12Asp.net配合easyui實現(xiàn)返回json數(shù)據實例
這篇文章主要介紹了Asp.net配合easyui實現(xiàn)返回json數(shù)據的方法,實例分析了Asp.net配合easyui返回json數(shù)據時出現(xiàn)的問題及解決方法,非常具有實用價值的技巧,需要的朋友可以參考下2014-12-12.NET Core 實現(xiàn)定時抓取網站文章并發(fā)送到郵箱
本片文章通過實例講述.NET Core 實現(xiàn)定時抓取博客園首頁文章信息并發(fā)送到郵箱這個功能,對此有興趣的朋友參考學習下。2018-02-02使用VSCode開發(fā)和調試.NET Core程序的方法
這篇文章主要介紹了使用VSCode開發(fā)和調試.NET Core程序的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05asp.Net 中獲取一周第一天,一月第一天等實現(xiàn)代碼
.Net中獲取一周第一天、最后一天,一月第一天、最后一天2009-12-12