濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > asp.net基于HashTable實(shí)現(xiàn)購(gòu)物車(chē)的方法

asp.net基于HashTable實(shí)現(xiàn)購(gòu)物車(chē)的方法

熱門(mén)標(biāo)簽:塔城代理外呼系統(tǒng) 地圖定位圖標(biāo)標(biāo)注 代理接電話機(jī)器人如何取消 地圖標(biāo)注專業(yè)團(tuán)隊(duì) 天心智能電銷(xiāo)機(jī)器人 400電話辦理哪家性價(jià)比高 地圖標(biāo)注的公司有哪些 遂寧市地圖標(biāo)注app 濮陽(yáng)外呼電銷(xiāo)系統(tǒng)怎么樣

本文實(shí)例講述了asp.net基于HashTable實(shí)現(xiàn)購(gòu)物車(chē)的方法。分享給大家供大家參考,具體如下:

//用戶購(gòu)買(mǎi)商品時(shí)
if (e.CommandName.ToLower() == "buy") 
{
 //判斷用戶購(gòu)物車(chē)是否為空 如果為空則分配一個(gè)
 Hashtable table;
 if (Session["car"] == null)
 {
  table = new Hashtable();
 }
 else
 {
  //用戶購(gòu)物車(chē)己存在 則取出數(shù)據(jù)
  table = Session["car"] as Hashtable;
 }
 //如果用戶購(gòu)物車(chē)中不包括該商品信息 則添加一個(gè)新商品
 if (!table.Contains(e.CommandArgument))
 {
  table.Add(e.CommandArgument, 1);//添加一個(gè)新商品 數(shù)量為1
 }
 else 
 {
  //如果購(gòu)物車(chē)己存在該商品信息 則將該商品的數(shù)量加1 根據(jù)HashTable的鍵獲取相對(duì)應(yīng)的值
  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">您還沒(méi)有購(gòu)物呢?趕快購(gòu)物吧!/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的值 并將它設(shè)置為T(mén)able1的主鍵
  DataColumn[] keys = { Table1.Columns["pid"]};
  Table1.PrimaryKey = keys;
  foreach (string key in table.Keys)
  {
   Table1.Rows.Find(key)["shuliang"] = table[key];//根據(jù)鍵獲取值 商品的數(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 從購(gòu)物車(chē)中刪除一條商品信息
protected void MyGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
  Hashtable table;
  if (Session["car"] == null)
  {
   table = new Hashtable();
  }
  else
  {
   table = Session["car"] as Hashtable;
  }
  //如果點(diǎn)擊刪除按鈕 則從購(gòu)物車(chē)中移除該商品信息
  if (e.CommandName.ToLower() == "delete")
  {
   if (table.ContainsKey(e.CommandArgument))
   {
    //從HashTable中移除該商品的信息(商品編號(hào)) 鍵:為商品編號(hào) 值為:商品數(shù)量
    table.Remove(e.CommandArgument);
   }
   Msg.Text = (string)e.CommandArgument;
  }
  Session["car"] = table;
  //調(diào)用方法
  shoplist();
}
#endregion

希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • asp.net 實(shí)現(xiàn)自定義Hashtable (.net)
  • asp.net Hashtable 遍歷寫(xiě)法
  • asp.net基于session實(shí)現(xiàn)購(gòu)物車(chē)的方法
  • ASP.NET購(gòu)物車(chē)實(shí)現(xiàn)過(guò)程詳解
  • asp.net 購(gòu)物車(chē)的實(shí)現(xiàn)淺析
  • asp.net 購(gòu)物車(chē)實(shí)現(xiàn)詳細(xì)代碼

標(biāo)簽:河南 宜春 重慶 麗江 婁底 本溪 汕頭 吉林

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net基于HashTable實(shí)現(xiàn)購(gòu)物車(chē)的方法》,本文關(guān)鍵詞  asp.net,基于,HashTable,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net基于HashTable實(shí)現(xiàn)購(gòu)物車(chē)的方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于asp.net基于HashTable實(shí)現(xiàn)購(gòu)物車(chē)的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    鲁甸县| 蕲春县| 东明县| 罗江县| 牡丹江市| 宝坻区| 安多县| 巴林右旗| 唐河县| 绥宁县| 竹溪县| 石门县| 韶关市| 惠州市| 恭城| 石楼县| 周宁县| 盈江县| 岳阳县| 射阳县| 陕西省| 大城县| 科技| 庆阳市| 松阳县| 永城市| 淮滨县| 吴江市| 上饶市| 东光县| 融水| 岢岚县| 苍南县| 新丰县| 勃利县| 德钦县| 庆云县| 正宁县| 马公市| 宜章县| 营山县|