asp.net method for realizing shopping cart based on hashtable-practical skills

Source: Internet
Author: User

This article illustrates the ASP.NET approach to implementing a shopping cart based on Hashtable. Share to everyone for your reference, specific as follows:

If the user buys the product if (e.commandname.tolower () = = "buy") {//judge whether the user shopping cart is empty if empty then assign a Hashtable table;
 if (session["car"] = = null) {table = new Hashtable ();
 else {//the user's cart already exists then remove the data table = session["Car"] as Hashtable; ///If the user does not include the product information in the cart, add a new product if (!table. Contains (e.commandargument)) {table. Add (e.commandargument, 1);//Adding a new product quantity of 1} else {//if the cart already exists this product information will add the quantity of the goods to 1 according to the Hashtable key to get the corresponding value int count = Conv Ert. ToInt32 (Table[e.commandargument].
  ToString ());
 Add 1 table[e.commandargument to the quantity of the commodity] = (count + 1);
 //Save commodity information session["car" = table;
Response.Redirect ("shoppingcar.aspx");
  }//commodity information list 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" > You haven't shopped yet?
  Hurry up and go shopping!</b> "; } string[] Arrkey = new string[table.
  Count]; int[] Arrval = new int[table.
  Count]; Table.
  Keys.copyto (Arrkey, 0); Table.
  Values.copyto (arrval, 0);
  Define string Formation (' 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"));
  Gets the value of the PID and sets it to the Table1 primary key datacolumn[] keys = {table1.columns["pid"};
  Table1.primarykey = keys; foreach (string key in table.) Keys) {Table1.Rows.Find (key) ["Shuliang"] = table[key];//Gets the number of value items based on the key} Table1.Columns.Add (New DataColumn ("Zongji
  A ", 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 Remove a product information from the cart protected void Mygrid_rowcommand (object sender, Gridviewcommandeventargs e) {Hashtable table;
  if (session["car"] = = null) {table = new Hashtable ();
  else {table = session["Car"] as Hashtable; If you click the Delete button, remove the product information from the shopping cart if (e.commandname.tolower () = "Delete") {if table. ContainsKey (e.commandargument)) {//Remove the item's information (item number) key from the Hashtable: The item number value is: Quantity table.
   Remove (e.commandargument);
  } Msg.text = (string) e.commandargument;
  } session["Car" = table;
Call Method Shoplist ();

 } #endregion

I hope this article will help you with ASP.net programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.