asp. net Implementation of Shopping Cart Based on HashTable
- 2021-07-10 19:21:56
- OfStack
In this paper, an example of asp. net based on HashTable to achieve shopping cart method. Share it for your reference, as follows:
// When a user buys a product,
if (e.CommandName.ToLower() == "buy")
{
// Judge whether the shopping cart of the user is empty or not If empty, assign 1 A
Hashtable table;
if (Session["car"] == null)
{
table = new Hashtable();
}
else
{
// User shopping cart already exists The data is fetched
table = Session["car"] as Hashtable;
}
// If the item information is not included in the user's shopping cart Add the 1 New merchandise
if (!table.Contains(e.CommandArgument))
{
table.Add(e.CommandArgument, 1);// Add 1 New merchandise Quantity is 1
}
else
{
// If the item information already exists in the shopping cart Add the quantity of the item 1 According to HashTable Gets the corresponding value with the key of
int count = Convert.ToInt32(table[e.CommandArgument].ToString());
// Add the quantity of the commodity 1
table[e.CommandArgument] = (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 been shopping yet? Go shopping quickly !</b>";
}
string[] Arrkey = new string[table.Count];
int[] ArrVal = new int[table.Count];
table.Keys.CopyTo(Arrkey, 0);
table.Values.CopyTo(ArrVal, 0);
// Definition string 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")));
// Get pid Value of And set it to Table1 Primary key of
DataColumn[] keys = { Table1.Columns["pid"]};
Table1.PrimaryKey = keys;
foreach (string key in table.Keys)
{
Table1.Rows.Find(key)["shuliang"] = table[key];// Get the value according to the key Quantity of goods
}
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 Delete from shopping cart 1 Article commodity information
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 is removed from the shopping cart
if (e.CommandName.ToLower() == "delete")
{
if (table.ContainsKey(e.CommandArgument))
{
// From HashTable Remove the information of this item from the ( Commodity number ) Key : Be the commodity number Value is : Quantity of goods
table.Remove(e.CommandArgument);
}
Msg.Text = (string)e.CommandArgument;
}
Session["car"] = table;
// Invoke method
shoplist();
}
#endregion
I hope this article is helpful to everyone's asp. net programming.