CodeFrom ASP. net2.0 Development Guide
A very useful feature can be saved for future reference.
Related Files in Resource Manager
App_code contains a class file shopingcart, which uses a hash table to store the entity of the purchased item
The default page contains two gridview controls.
Class relationship
Web. config
<? XML version = "1.0" ?>
< Configuration Xmlns = "Http://schemas.microsoft.com/.NetConfiguration/v2.0" >
< Appsettings />
< Connectionstrings >
< Add Name = "Northwindconnectionstring" Connectionstring = "Data Source = localhost; initial catalog = northwind; Integrated Security = true"
Providername = "System. Data. sqlclient" />
</ Connectionstrings >
< System . Web >
< Profile >
< Properties >
< Add Name = "Shoppingcart" Type = "Shoppingcart" Serializeas = "Binary" />
</ Properties >
</ Profile >
< Authorization >
< Deny Users = "? " />
</ Authorization >
< Authentication Mode = "Forms" >
< Forms Loginurl = "Login. aspx" > </ Forms >
</ Authentication >
< Compilation Debug = "False" />
</ System. Web >
</ Configuration >
Default. aspx. CS
Using System;
Using System. Data;
Using System. configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Public Partial Class _ Default: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
If(!Page. ispostback)
{
Bindshoppingcart ();
}
}
// Display the information of the shopping cart saved in the profile object
Protected Void Bindshoppingcart ()
{
// If the item in the shopping cart stored in the profile is not empty, data is bound and the total price is calculated.
If (Profile. shoppingcart ! = Null )
{
Cartgrid. datasource = Profile. shoppingcart. cartitems;
Cartgrid. databind ();
Lbltotal. Text = " Total price: " + Profile. shoppingcart. Total. tostring ( " C " );
}
}
// Add the selected item to the shopping cart
Protected Void Addcartitem ( Object Sender, eventargs E)
{
// Obtain selected data rows
Gridviewrow row = Productgrid. selectedrow;
// Obtains the value of the primary key ID.
Int ID = ( Int ) Productgrid. selecteddatakey. value;
// Get product name
String name = Row. cells [ 1 ]. Text;
// Get product unit price
Decimal Price = Decimal. parse (row. cells [ 2 ]. Text, system. Globalization. numberstyles. Currency );
// If the shopping cart object stored in profile is null, a corresponding object is created.
If (Profile. shoppingcart = Null )
{
Profile. shoppingcart= NewShoppingcart ();
}
// Use the data obtained above to add the selected item to the profile object shopping cart.
Profile. shoppingcart. additem (ID, name, price );
// Display shopping cart data
Bindshoppingcart ();
}
// Delete the selected item from the shopping cart
Protected Void Removecartitem ( Object Sender, eventargs E)
{
// Obtains the primary key ID of the selected item.
Int ID = ( Int ) Cartgrid. selecteddatakey. value;
// Use Id to delete the item from the profile object shopping cart.
Profile. shoppingcart. removeitem (ID );
// Display shopping cart data
Bindshoppingcart ();
}
}
Code download: http://files.cnblogs.com/hide0511/shoppingcart.rar