asp.net shopping Cart Implementation Detail Code _ Practical Skills
Last Update:2017-01-18
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= "$" border= "0" cellspacing= "0" cellpadding= "0" > <tr> <td>
<asp:datagrid id= "SHOPPINGCARTDLT" runat= "Server" width= "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= "Product 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 (yuan)"/> </Columns> </asp:datagrid></td > </tr> </table> <br> <table width= "$" border= "0" cellspacing= "0" cellpadding= "0" > <tr& Gt <td>
<asp:button id= "Update" runat= "server" text= "Updating my 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>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
{
A brief description of the <summary>///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 to see if the user has logged in.
if (! IsPostBack)
{
if (request.params["mode"]== "view")//detect if the cart is viewed directly.
{
Viewshoppingcart ();
Caculator ();
}
if (request.params["ProductID"]!=null| | request.params["ProductID"]!= "")
{
addproid=request["ProductID"];
Updateshoppingcart ();
Caculator ();
}
}
Place user code here to initialize page
}
public void createcarttable ()//Create 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 ();
Line, has been logged off, 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 function createcarttable () creates a new DataTable Writeshoppingcart ();
}
Else
{
If there is already a product in the shopping basket, you need to update the shopping information table DataTable and set the bar to Shoppingcartdlt Writeshoppingcart ();
}
}
public void Viewshoppingcart ()//View Cart
{
if (session["mycarttable"]!=null)
{
DataTable viewtable=new DataTable ("Nowcarttable");
Viewtable= (DataTable) session["mycarttable"];
Shoppingcartdlt.datasource = Viewtable.defaultview;
Shopping cart stick to shoppingcartdlt shoppingcartdlt.databind ();
}
}
public void Writeshoppingcart ()
{
if (request.params["mode"]!= "view")/check if the cart is viewed directly, if you view it directly, you will not write mycarttable
{
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))//judgment in the shopping information table, whether there is currently put into the product. if (Nowprodid==int32.parse (addproid))
{
Hasone=true;
}
Else
{
i++;
}
}
if (Hasone)
{
If the product is already there, hasone=true, change the data row DataRow olddr;
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 you don't have the product, add a new row to the table. DataRow NEWDR;
Double UNITP;
String strcon= "Provider=Microsoft.Jet.OLEDB.4.0;
Data source= "+server.mappath (configurationsettings.appsettings[" 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;
The first time to read the library, so the total price and the unit price is the same. newdr[5]= "0";
NOWTABLE.ROWS.ADD (NEWDR);
Myconnection.close ();
}
Shoppingcartdlt.datasource = Nowtable.defaultview;
The updated DataTable Stick is fixed to SHOPPINGCARTDLT shoppingcartdlt.databind ();
session["mycarttable"] = nowtable;
Re-Save the updated DataTable
}
}
public void Caculator ()
{
if (session["mycarttable"]!=null)//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 the goods in the shopping 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 deletion mark 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 generated #region The Web Forms Designer override protected void OnInit (EventArgs e)
{
CodeGen: This call is required for the ASP.net Web forms Designer. InitializeComponent ();
Base. OnInit (e);
}
<summary>///Designer supports the desired method-do not use the Code Editor to modify the contents 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");
}
}
}