The shopping cart function is implemented for registered users.

Source: Internet
Author: User
Tags connectionstrings

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

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.