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;
}