This article illustrates the ASP.NET approach to implementing a shopping cart based on session. Share to everyone for your reference, specific as follows:
Add Shopping cart protected void Linkbutton2_click (object sender, EventArgs e) {DataTable dt = null; User purchase if no login is prompted to login if (session["userName"] = = null) {Common.MessageBox.ShowAndRedirectTo (this.
Page, "Please log in before you buy the merchandise!", "Login.aspx"; else {//If you have already logged in according to the item number to obtain the details of the product (return entity Class)//return information have commodity number commodity name commodity market price Commodity size (siz) picture (image) weight Total price purchase number
Total amount of money to be paid gross weight model.product Model = new Model.product ();
Obtain the Product details Model.id = (int) Request.QueryString.GetValues ("UID") [0] by the item number;
System.Data.SqlClient.SqlDataReader SDR=DAL.PRODUCT.DRPR (model);
if (session["shopcar"] = = null) {dt = new DataTable (); Commodity number dt.
Columns.Add ("id", typeof (Int32)); Commodity price (the price of the site) dt.
Columns.Add ("Localprice", typeof (Double)); Commodity release Price dt.
Columns.Add ("Posttime", typeof (String)); Purchase Quantity dt.
Columns.Add ("Buycount", typeof (Int32)); Commodity price dt.
Columns.Add ("Sumprice", typeof (Int32)); Color dt.
Columns.Add ("Color", typeof (String)); Big goodsSmall dt.
Columns.Add ("Size", typeof (String)); Product Name DT.
Columns.Add ("title", typeof (String)); Product Type dt.
Columns.Add ("Cate", typeof (String)); Product Picture dt.
Columns.Add ("image", typeof (String)); Commodity Time dt.
Columns.Add ("Only", typeof (String)); Commodity weight dt.
Columns.Add ("Weight", typeof (String)); Total weight of merchandise dt.
Columns.Add ("Sumweight", typeof (String));
else {//If the shopping cart is not empty, just judge if the item's information exists in the cart if present, add the quantity of the product to 1 dt = session["Shopcar"] as DataTable; for (int j = 0; j < dt.) Rows.Count; J + +) {///If the product is already in existence by the number of the product, add the quantity of the product to 1 if (dt. rows[j]["id"]. ToString () = model.id.ToString () && convert.tostring (dt. rows[i]["color"] = = D_color. Text) {////If the product is already there, buy the quantity plus 1 dt. rows[j]["Buycount"] = Convert.ToInt32 (dt.
rows[j]["Buycount"]) + 1;
Session Save Cart session["Shopcar"] = DT;
Response.Redirect ("shoppingcar.aspx");
Return }///If the product information does not exist in the shopping cart, add a new product information DataRow ROw = dt.
NewRow (); if (Dr. Read ()) {//The weight of the commodity row["weight"] = sdr["_weight"].
ToString (); The total weight of the product row["sumweight"] = sdr["_weight"].
ToString ();
Commodity purchase time row["only"] = DateTime.Now.ToString ("Yyyymmddhhmmssfff"); Product model row["size"] = _size.
Text;
Product picture row["image"] = Image_1.imageurl; Commodity type name row["cate"] = Cate_name.
Text;
Commodity name row["title"] = Title_l.text; Product color row["COLOR"] = D_color.
Text; Item number row["id" = Convert.ToInt32 (dr["_id"].
ToString ()); Commodity site Price row["localprice"] = convert.todouble (sdr["_localprice").
ToString ()); Product release Time row["posttime"] = convert.tostring (sdr["_posttime").
ToString ());
The purchase quantity defaults to 1 row["buycount"] = 1; Commodity price row["sumprice"] = convert.todouble (sdr["_localprice").
ToString ()); Dt.
Rows.Add (row); } Dr.
Close ();
Save commodity information session["DT"] = DT;
Response.Redirect ("shoplist.aspx"); }///Get merchandise information (total merchandise weight price) private void Getcountinfo () {DatataBLE dt = null;
if (session["Shopcar"]!= null) {//display hint information label1.visible = true;
else {dt = session["Shopcar"] as DataTable; for (int j = 0; j < dt.) Rows.Count; J + + to obtain the total price of a single item (calculated by: The quantity of the product multiplied by the price of the commodity) dt. rows[j]["Sumprice"] = Convert.ToInt32 (dt. rows[j]["Localprice"]*convert.toint32 (dt.
rows[j]["Buycount"]); Gets the total weight of a single item (calculation formula: The quantity of merchandise purchased multiplied by the single weight of the commodity) dt. rows[j]["sumweight"] = Convert.ToInt32 (dt. rows[j]["Weight"] * convert.toint32 (dt.
rows[j]["Buycount"]);
Save information session["Shopcar"] = DT;
Displays all merchandise information for the user's cart Showshoppingcarinfo ();
}}///<summary>///displays the purchased merchandise information///</summary> private void Showshoppingcarinfo () {DataTable dt = null;
Define get total commodity price variable double wholeprice = 0;
Defines the total weight variable for obtaining merchandise double wholeweight = 0;
User has purchased if (session["Shopcar"]!= null) {dt = session["Shopcar"] as DataTable; for (int j = 0; j < dt.) Rows.Count; J + + = Get all total Merchandise prices Wholeprice + + convert.todouble (dt. Rows[j]
["Sumprice"]); Get the total weight of the merchandise wholeweight + + = convert.todouble (dt.
rows[j]["Sumweight"]); }//Total Price wprice. Text = Wholeprice.
ToString (); Total weight wweight. Text = Wholeweight.
ToString ();
Save the total price of the goods session["wholeprice"] = Wholeprice;
The total weight of the store session["wholeweight"] = wholeweight; The total number of commodities is wholeprocount. Text = convert.tostring (dt.
Rows.Count); The total item session["Wholeprocount"] = Wholeprocount.
Text; If the number of items is empty time if (dt.
Rows.Count = = 0) {//show user does not buy product label1.visible = true;
} else {//If no purchase is purchased then display the message label1.visible = true;
Gridview1.datasource = null;
Gridview1.databind ();
}
}
Remove a product from the shopping cart: Row_command
if (e.commandname== "del")
{
DataTable dt=session["Shopcar"] as DataTable;
for (int j=0;j<dt. rows.count;j++) {
if (dt. rows[j]["only"]==e.commandargument)
{
dt. Rows.remove (j);}}
I hope this article will help you to ASP.net program design.