Detailed implementation code of asp.net shopping cart
Last Update:2018-12-08
Source: Internet
Author: User
<% @ Page language = "c #" Codebehind = "shoppingcart. aspx. cs" AutoEventWireup = "false" Inherits = "myshop. shoppingcart" %> <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML> <HEAD>
<Title> shoppingcart
</Title>
<Meta http-equiv = "Content-Type" content = "text/html;
Charset = gb2312 "> <LINK href =" mycss.css "type =" text/css "rel =" stylesheet ">
<Meta name = "vs_defaultClientScript" content = "JavaScript">
<Meta name = "vs_targetSchema" content = "http://schemas.microsoft.com/intellisense/ie5"> </HEAD>
<Body> <center>
<Form id = "Form1" runat = "server"> <table width = "500" border = "0" cellspacing = "0" cellpadding = "0"> <tr> <td>
<Asp: dataGrid id = "ShoppingCartDlt" runat = "server" Width = "500" BackColor = "white" BorderColor = "black" ShowFooter = "false" CellPadding = "3" CellSpacing = "0 "Font-Name =" Verdana "Font-Size =" 8pt "HeaderStyle-BackColor =" # cecfd6 "AutoGenerateColumns =" false "MaintainState =" true "> <Columns>
<Asp: TemplateColumn HeaderText = "delete">
<ItemTemplate> <center>
<Asp: CheckBox id = "chkProductID" runat = "server"/> </center>
</ItemTemplate> </asp: TemplateColumn>
<Asp: BoundColumn DataField = "ProdID" HeaderText = "ID"/>
<Asp: BoundColumn DataField = "ProName" HeaderText = "item name"/>
<Asp: BoundColumn DataField = "UnitPrice" HeaderText = "unit price"/>
<Asp: TemplateColumn HeaderText = "quantity">
<ItemTemplate>
<Asp: TextBox id = "CountTb" runat = "server" Text = '<% # DataBinder. eval (Container. dataItem, "ProdCount") %> '> </asp: TextBox>
</ItemTemplate> </asp: TemplateColumn>
<Asp: BoundColumn DataField = "TotalPrice" HeaderText = "subtotal (RMB)"/> </Columns> </asp: dataGrid> </td> </tr> </table> <br> <table width = "500" border = "0" cellspacing = "0" cellpadding = "0"> <tr> <td>
<Asp: Button id = "update" runat = "server" Text = "update my shopping cart" CssClass = "button2"/> </td> <td>
<Asp: Button id = "CheckOut" runat = "server" Text = "Settlement" CssClass = "button5"/>
<Input type = "button" name = "close2" value = "continue shopping" onClick = "window. close ();
Return false;
"Class =" button2 "> </td> <td align =" right "> <br>
<Asp: Label id = "label" runat = "server" Width = "100px" Visible = "True" ForeColor = "# FF8080" Height = "18px"> </asp: label> </td> </tr> </table>
</Form> </center>
</Body> </HTML> ================================== ========================================================== ========================================================= ========================================================== ====================================
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Web. SessionState;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Data;
Using System. Data. OleDb;
Using System. Configuration;
Namespace myshop
{
/// <Summary> /// summary of shoppingcart. // </summary> public class shoppingcart: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. DataGrid ShoppingCartDlt;
Protected System. Web. UI. WebControls. Button update;
Protected System. Web. UI. WebControls. Button CheckOut;
Protected System. Web. UI. HtmlControls. HtmlForm Form1;
Protected System. Web. UI. WebControls. Label label;
Protected System. Web. UI. WebControls. CheckBox chkProductID;
Protected System. Web. UI. WebControls. TextBox txtCount;
Protected System. Web. UI. WebControls. TextBox CountTb;
String AddProID;
Private void Page_Load (object sender, System. EventArgs e)
{
Try
{
If (Session ["logon"]! = "Yes" | Session ["username"] = null)
{
Response. Redirect ("error.htm ");
}
}
Catch
{
Response. Redirect ("error.htm ");
}
/////////// Check whether the user has logged on.
If (! IsPostBack)
{
If (Request. Params ["mode"] = "view") // check whether the shopping cart is directly viewed.
{
ViewShoppingCart ();
Caculator ();
}
If (Request. Params ["productID"]! = Null | Request. Params ["productID"]! = "")
{
AddProID = Request ["productID"];
UpdateShoppingCart ();
Caculator ();
}
}
// Place user code here to initialize the page
}
Public void CreateCartTable () // create a shopping cart
{
DataSet ds = new DataSet ();
DataTable newDT = new DataTable ("CartTable ");
Ds. Tables. Add (newDT );
DataColumn newDC;
NewDC = new DataColumn ("ProdID", System. Type. GetType ("System. Int32 "));
Ds. Tables ["CartTable"]. Columns. Add (newDC );
NewDC = new DataColumn ("ProdCount", System. Type. GetType ("System. Int32 "));
NewDC. DefaultValue = 1;
Ds. Tables ["CartTable"]. Columns. Add (newDC );
NewDC = new DataColumn ("ProName", System. Type. GetType ("System. String "));
Ds. Tables ["CartTable"]. Columns. Add (newDC );
NewDC = new DataColumn ("UnitPrice", System. Type. GetType ("System. Double "));
Ds. Tables ["CartTable"]. Columns. Add (newDC );
NewDC = new DataColumn ("TotalPrice", System. Type. GetType ("System. Double "));
Ds. Tables ["CartTable"]. Columns. Add (newDC );
NewDC = new DataColumn ("IsDeleted", System. Type. GetType ("System. Int32 "));
NewDC. DefaultValue = 0;
// NewDR [5] = "0" in public void WriteShoppingCart ";
Row. It has been deregistered. ds. Tables ["CartTable"]. Columns. Add (newDC );
Session ["myCartTable"] = newDT;
ShoppingCartDlt. DataSource = ds. Tables ["CartTable"]. DefaultView;
ShoppingCartDlt. DataBind ();
}
Public void UpdateShoppingCart ()
{
If (Session ["myCartTable"] = null) // Session ["myCartTable"] = null
{
CreateCartTable ();
// Call the CreateCartTable () function to create a DataTable WriteShoppingCart ();
}
Else
{
// If there are items in the shopping blue, update the shopping information table able and set it to ShoppingCartDlt WriteShoppingCart ();
}
}
Public void ViewShoppingCart () // view the shopping cart
{
If (Session ["myCartTable"]! = Null)
{
DataTable viewTable = new DataTable ("nowCartTable ");
ViewTable = (DataTable) Session ["myCartTable"];
ShoppingCartDlt. DataSource = viewTable. DefaultView;
// Set the shopping car to ShoppingCartDlt. DataBind ();
}
}
Public void WriteShoppingCart ()
{
If (Request. Params ["mode"]! = "View") // check whether the shopping cart is directly viewed. If the shopping cart is directly viewed, MYCARTTABLE is no longer written.
{
DataTable nowTable = new DataTable ("nowCartTable ");
NowTable = (DataTable) Session ["myCartTable"];
Int pn = nowTable. Rows. Count;
Int I = 0;
Bool hasone = false;
Int nowProdID;
While (I <pn &&! Hasone)
{
NowProdID = Int32.Parse (nowTable. Rows [I] [0]. ToString ());
If (nowProdID = Int32.Parse (AddProID) // determines whether the shopping information table contains the currently placed goods. if (nowProdID = Int32.Parse (AddProID ))
{
Hasone = true;
}
Else
{
I ++;
}
}
If (hasone)
{
// If the item already exists, hasone = true. Change the DataRow oldDR of the Data row;
OldDR = nowTable. Rows [I];
OldDR ["ProdCount"] = Int32.Parse (oldDR ["ProdCount"]. ToString () + 1;
OldDR ["TotalPrice"] = Int32.Parse (oldDR ["ProdCount"]. ToString () * Double. Parse (oldDR ["UnitPrice"]. ToString ());
}
Else
{
// If This item does not exist, add a new row in the table. DataRow newDR;
Double unitp;
String strcon = "provider = Microsoft. jet. OLEDB.4.0;
Data Source = "+ Server. MapPath (ConfigurationSettings. receivettings [" MDBpath2 "]) + ";
";
OleDbConnection myConnection = new OleDbConnection (strcon );
String strSQL = "select * from pro where product_id =" + AddProID + "";
OleDbDataAdapter myCommand = new OleDbDataAdapter (strSQL, myConnection );
DataSet ds = new DataSet ();
MyCommand. Fill (ds, "AddP ");
NewDR = nowTable. NewRow ();
NewDR [0] = AddProID;
NewDR [2] = ds. Tables ["Addp"]. Rows [0] ["product_name"]. ToString ();
Unitp = Double. Parse (ds. Tables ["AddP"]. Rows [0] ["product_memprice"]. ToString ());
// Member price newDR [3] = unitp;
NewDR [4] = unitp;
// Read the database for the first time, so the total price is the same as the unit price. // newDR [5] = "0 ";
NowTable. Rows. Add (newDR );
MyConnection. Close ();
}
ShoppingCartDlt. DataSource = nowTable. DefaultView;
// Set the updated able to ShoppingCartDlt. DataBind ();
Session ["myCartTable"] = nowTable;
// Re-Save the updated able
}
}
Public void Caculator ()
{
If (Session ["myCartTable"]! = Null) // whether the shopping cart is empty
{
Int h;
Double TotalPri;
TotalPri = 0;
DataTable nowTable3 = new DataTable ("nowCartTable3 ");
NowTable3 = (DataTable) Session ["myCartTable"];
If (nowTable3.Rows. Count> 0) // returns whether there is any goods in the cart.
{
For (h = 0;
H <= nowTable3.Rows. Count-1;
H ++)
{
TotalPri = TotalPri + Int32.Parse (nowTable3.Rows [h] [4]. ToString ());
// Double. Parse (string) TotalText. Text );
}
Label. Text = "Total:" + TotalPri. ToString () + "Yuan ";
}
}
}
Public void Update ()
{
Int I;
Int j;
Int k;
ArrayList deleteItem = new ArrayList (10 );
DataGridItem _ item;
J = 0;
Int deleteid;
K = 0;
DataTable nowTable2 = new DataTable ("nowCartTable2 ");
NowTable2 = (DataTable) Session ["myCartTable"];
For (I = 0;
I <= this. ShoppingCartDlt. Items. Count-1;
I ++)
{
_ Item = this. ShoppingCartDlt. Items [I];
TextBox CountText = (TextBox) this. ShoppingCartDlt. Items [I]. Cells [4]. FindControl ("CountTb ");
// Controls [1];
// _ Item. FindControl ("CountTb ");
CheckBox ProductIDCheck = (CheckBox) _ item. FindControl ("chkProductID ");
NowTable2.Rows [I] [1] = Int32.Parse (CountText. Text. ToString ());
NowTable2.Rows [I] [4] = Int32.Parse (nowTable2.Rows [I] [1]. ToString () * Double. Parse (nowTable2.Rows [I] [3]. ToString ());
If (ProductIDCheck. Checked)
{
NowTable2.Rows [I] [5] = 1;
// Add the delete tag 1 j = j + 1;
}
}
String strExpr = "IsDeleted> 0 ";
// Http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/cpref/html/frlrfSystemDataDataTableClassSelectTopic. asp DataRow [] foundRows = nowTable2.Select (strExpr );
For (int m = 0;
M <foundRows. Length;
M ++)
{
// Console. WriteLine (foundRows [I] [0]);
FoundRows [m]. Delete ();
}
ShoppingCartDlt. DataSource = nowTable2.DefaultView;
ShoppingCartDlt. DataBind ();
Session ["myCartTable"] = nowTable2;
Caculator ();
}
# Code override protected void OnInit (EventArgs e) generated by region Web forms designer)
{
/// CODEGEN: This call is required by the asp. NET Web form designer. // InitializeComponent ();
Base. OnInit (e );
}
/// <Summary> /// the designer supports the required methods-do not use the code editor to modify the content of this method. /// </summary> private void InitializeComponent ()
{
This. update. Click + = new System. EventHandler (this. update_Click );
This. CheckOut. Click + = new System. EventHandler (this. CheckOut_Click );
This. Load + = new System. EventHandler (this. Page_Load );
}
# Endregion
Private void update_Click (object sender, System. EventArgs e)
{
Update ();
}
Private void CheckOut_Click (object sender, System. EventArgs e)
{
Update ();
Response. Redirect ("checkout. aspx ");
}
}
}