Asp.net implements the shopping cart Method Based on HashTable,
This article describes how to implement a shopping cart in asp.net Based on HashTable. We will share this with you for your reference. The details are as follows:
// If (e. commandName. toLower () = "buy") {// determines whether the user's shopping cart is empty. if it is empty, a Hashtable table is allocated. if (Session ["car"] = null) {table = new Hashtable ();} else {// if the user's shopping cart already exists, retrieve the data table = Session ["car"] as Hashtable ;} // Add a new item if (! Table. contains (e. commandArgument) {table. add (e. commandArgument, 1 ); // Add a new item quantity to 1} else {// If the cart already has this item information, add the number of items to 1 and obtain the corresponding value int count based on the HashTable key = Convert. toInt32 (table [e. commandArgument]. toString (); // Add 1 table [e. commandArgument] = (count + 1);} // Save the item information Session ["car"] = table; Response. redirect ("shoppingcar. aspx ");} // product 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 have not purchased any products yet? Buy now! </B> ";} string [] Arrkey = new string [table. count]; int [] ArrVal = new int [table. count]; table. keys. copyTo (Arrkey, 0); table. values. copyTo (ArrVal, 0); // define the string to form ('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 "); // obtain the pid value and set it as the primary key DataColumn [] keys = {Table1.Columns [" pid "]} of Table1; Table1.PrimaryKey = keys; foreach (string key in table. keys) {Table1.Rows. find (key) ["shuliang"] = table [key]; // the number of items that are retrieved by the 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 deletes a item 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, the item information will be removed from the shopping cart if (e. commandName. toLower () = "delete") {if (table. containsKey (e. commandArgument) {// information (item number) for removing this item from HashTable key: item number value: item quantity table. remove (e. commandArgument);} Msg. text = (string) e. commandArgument;} Session ["car"] = table; // call the shoplist () ;}# endregion
I hope this article will help you design your asp.net program.
Articles you may be interested in:
- Asp.net implement custom Hashtable (. net)
- Asp.net Hashtable Traversal
- Asp.net implements Shopping Cart Based on session
- Detailed description of ASP. NET shopping cart implementation process
- Implementation of asp.net shopping cart
- Detailed implementation code of asp.net shopping cart