<% @ 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"/> & nbsp; <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>
========================================================== ========================================================== ========
Above is the HTML page section
========================================================== ========================================================== ============
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 in.
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, canceled,
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 cart to shoppingcartdlt.
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) // you can check whether the current item is included in the shopping information table. If (nowprodid = int32.parse (addproid ))
{
Hasone = true;
}
Else
{
I ++;
}
}
If (hasone)
{// If the item already exists, hasone = true to 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 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 (); // membership 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.
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 Delete flag 1
J = J + 1;
}
}
String strexpr = "isdeleted> 0"; // A 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 by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// 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
/// 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 ");
}
}
}