Asp.net implements the shopping cart Method Based on the session, and the session shopping cart

Source: Internet
Author: User

Asp.net implements the shopping cart Method Based on the session, and the session shopping cart

This article describes how to implement a shopping cart in asp.net Based on session. We will share this with you for your reference. The details are as follows:

// Add the shopping cart protected void LinkButton2_Click (object sender, EventArgs e) {DataTable dt = null; // if the user has not logged on to the purchased item, the user will be prompted to log on if (Session ["userName"] = null) {Common. messageBox. showAndRedirectTo (this. page, "Log on first and then purchase the product! "," Login. aspx ");} else {// if you have logged on, obtain the details of the product based on the received product number (returns the entity class) // The returned information includes the product ID, product name, product market price, product size (siz), image, total weight, total purchased quantity, total amount of money, product weight Model. product model = new Model. product (); // obtain the product details model based on the product ID. id = (int) Request. queryString. getValues ("uid") [0]; System. data. sqlClient. sqlDataReader sdr = DAL. product. drpr (model); if (Session ["ShopCar"] = null) {dt = new DataTable (); // item No. dt. column S. add ("id", typeof (Int32); // product price (this site price) dt. columns. add ("localprice", typeof (Double); // product release price dt. columns. add ("posttime", typeof (String); // purchase quantity dt. columns. add ("buycount", typeof (Int32); // total commodity price dt. columns. add ("sumprice", typeof (Int32); // color dt. columns. add ("color", typeof (String); // item size dt. columns. add ("size", typeof (String); // product name dt. columns. add ("title", typeof (String); // product type dt. columns. A Dd ("cate", typeof (String); // product image dt. columns. add ("image", typeof (String); // product time dt. columns. add ("only", typeof (String); // item weight dt. columns. add ("weight", typeof (String); // total item weight dt. columns. add ("sumweight", typeof (String ));} else {// If the shopping cart is not empty, just judge whether the item information exists in the shopping cart. If so, add 1 dt = Session ["ShopCar"] as DataTable; for (int j = 0; j <dt. rows. count; j ++) {// if the number of the item is read, the number of the item plus 1 if (Dt. rows [j] ["id"]. toString () = model. id. toString () & Convert. toString (dt. rows [I] ["color"]) = d_color.Text) {// if the product already exists, add 1 dt to the purchased quantity. rows [j] ["buycount"] = Convert. toInt32 (dt. rows [j] ["buycount"]) + 1; // Session save shopping cart Session ["ShopCar"] = dt; Response. redirect ("ShoppingCar. aspx "); return ;}}// if the item information does not exist in the shopping cart, add a new item information DataRow row = dt. newRow (); if (dr. read () {// item weight row ["weight"] = sdr ["_ we Ight "]. toString (); // total item weight row ["sumweight"] = sdr ["_ weight"]. toString (); // item purchase time row ["only"] = DateTime. now. toString ("yyyyMMddhhmmssfff"); // item model row ["size"] = _ size. text; // product image row ["image"] = Image_1.ImageUrl; // product type name row ["cate"] = cate_name.Text; // product name row ["title"] = title_l.Text; // product color row ["color"] = d_color.Text; // product id row ["id"] = Convert. toInt32 (dr ["_ id"]. toString (); // price of the product site row ["lo Calprice "] = Convert. toDouble (sdr ["_ localprice"]. toString (); // product release time row ["posttime"] = Convert. toString (sdr ["_ posttime"]. toString (); // The default purchase quantity is 1 row ["buycount"] = 1; // the total price of the product row ["sumprice"] = Convert. toDouble (sdr ["_ localprice"]. toString (); dt. rows. add (row);} dr. close (); // Save the product information Session ["dt"] = dt; Response. redirect ("shoplist. aspx ") ;}}// Obtain product information (total product weight total price) private void GetCountInfo () {DataTable Dt = null; if (Session ["ShopCar"]! = Null) {// display the prompt message Label1.Visible = true;} else {dt = Session ["ShopCar"] as DataTable; for (int j = 0; j <dt. rows. count; j ++) {// obtain the total price of a single commodity (formula: Number of commodities multiplied by the commodity price) dt. rows [j] ["sumprice"] = Convert. toInt32 (dt. rows [j] ["localprice"] * Convert. toInt32 (dt. rows [j] ["buycount"]); // obtain the total weight of a single item (formula: Number of purchased items multiplied by the individual weight of the item) dt. rows [j] ["sumweight"] = Convert. toInt32 (dt. rows [j] ["weight"] * Convert. toInt32 (dt. rows [j] ["Buycount"]); // Save the information Session ["ShopCar"] = dt; // display the information of all the items in the user's cart. ShowShoppingCarInfo ();}}} /// <summary> /// display the purchased item information /// </summary> private void ShowShoppingCarInfo () {DataTable dt = null; // define the Variable double wholePrice = 0; // define the Variable double wholeWeight = 0; // if (Session ["ShopCar"]! = Null) {dt = Session ["ShopCar"] as DataTable; for (int j = 0; j <dt. rows. count; j ++) {// obtain the total price of all products wholePrice + = Convert. toDouble (dt. rows [j] ["sumprice"]); // obtain the total weight of the product wholeWeight + = Convert. toDouble (dt. rows [j] ["sumweight"]);} // The total price of wprice. text = wholeprice. toString (); // total weight wweight. text = wholeweight. toString (); // Save the total price Session ["wholeprice"] = wholeprice; // Save the total weight Session ["wholeweight"] = wholeweight; // Total number of items wholeprocount. text = Convert. toString (dt. rows. count); // Save the total item Session ["wholeprocount"] = wholeprocount. text; // if the number of items is null, if (dt. rows. count = 0) {// display the user's unpurchased product Label1.Visible = true ;}} else {// if no product is purchased, the prompt message Label1.Visible = true is displayed; GridView1.DataSource = null; gridView1.DataBind ();}}

Remove a item 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 design your asp.net program.

Related Article

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.