Asp.net shopping cart

Source: Internet
Author: User

Protected void page_load (Object sender, eventargs E)
{
Displaycart ();
}

// Add to shopping cart
Protected void cb_id_checkedchanged (Object sender, eventargs E)
{
Checkbox cb = (checkbox) sender;
Datagriditem DGI = (datagriditem) CB. Parent. parent;
String name = (Label) DGI. findcontrol ("lb_name"). text;
Double price = convert. todouble (Label) DGI. findcontrol ("lb_price"). Text );

If (CB. Checked = true)
{
Cart c = new cart (name, price );

If (session ["cart"] = NULL)
{
Arraylist Al = new arraylist ();
Al. Add (C );
Session ["cart"] = Al;
}
Else
{
Arraylist Al = (arraylist) session ["cart"];
Al. Add (C );
Session ["cart"] = Al;
}
}
Displaycart ();
}

// Display the shopping cart
Protected void displaycart ()
{
If (session ["cart"]! = NULL)
{
Arraylist Al = (arraylist) session ["cart"];
Ienumerator Ie = Al. getenumerator ();

Datatable dt = new datatable ("product ");
Datacolumn DC1 = new datacolumn ("name ");
Datacolumn DC2 = new datacolumn ("price ");
DT. Columns. Add (DC1 );
DT. Columns. Add (DC2 );
While (ie. movenext ())
{
Cart c = (Cart) IE. Current;
Datarow DR = DT. newrow ();
Dr [0] = C. Name;
Dr [1] = C. Price. tostring ();
DT. Rows. Add (DR );
}
Datagrid2.datasource = DT;
Datagrid2.databind ();
}
Else
{
Datagrid2.datasource = NULL;
Datagrid2.databind ();
}
}

// Delete a single item
Protected void bt_delete_click (Object sender, eventargs E)
{
Button bt = (button) sender;
Datagriditem DGI = (datagriditem) BT. Parent. parent;
String name = (Label) DGI. findcontrol ("lb_name"). text;

If (session ["cart"]! = NULL)
{
Int I = 0;
Arraylist Al = (arraylist) session ["cart"];
Ienumerator Ie = Al. getenumerator ();
While (ie. movenext ())
{
Cart c = (Cart) IE. Current;
If (C. Name = Name)
{
Al. removeat (I );
Break;
}
I ++;
}
Session ["cart"] = Al;
}
Displaycart ();
}

// Clear the shopping cart
Protected void bt_cart_click (Object sender, eventargs E)
{
Session ["cart"] = NULL;
Displaycart ();
}

}

// Customize the shopping cart type
Public class cart
{
Private string c_name;
Private double c_price;

Public string name
{
Get
{
Return c_name;
}
Set
{
C_name = value;
}
}

Public double price
{
Get
{
Return c_price;
}
Set
{
C_price = value;
}
}

Public cart (string name, double price)
{
C_name = Name;
C_price = price;
}

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.