1. Attach the test database to the database management system; The Book_info in the database contains the following data:
2. Create a new website, copy the images folder to the website;
3, in Default.aspx, through the DataList control to display all the data in the database, in order to conduct the main sequence, 3 columns per row, click the Purchase button, the Product ID and quantity saved to the Hashtable, and the Hashtable placed into the session.
protected void Datalist1_itemcommand (object source, DataListCommandEventArgs e)
{
string id = e. Commandargument.tostring ();
Hashtable HT;
if (session["shopcar"] = = null)
{
ht = new Hashtable ();
Ht. ADD (ID, 1);
session["Shopcar"] = HT;
}
else
{
ht = (Hashtable) session["Shopcar"];
if (HT. Contains (ID)
{
int count = Int. Parse (Ht[id]. ToString ());
Ht[id] = count + 1;
session["Shopcar"] = HT;
Response.Write (count + 1);
}
else
{
ht. ADD (ID, 1);
session["Shopcar"] = HT;
}
4, in the Default.aspx to add a hyperlink, link to shopcart.aspx, in the shopcart.aspx display user purchase of merchandise information.
Tips:
A, the following variables are defined first in Shopcart:
Hashtable HT;
DataTable DT;
String connstring=@ "Datasource=.\sqlexpress;initial catalog=test;integrated security=true";
SqlConnection Conn;
SqlCommand cmd;
SqlDataReader SDR;
B, add a GridView to the page.
C, in Page_Load, the DT is instantiated and the columns are established.
protected void Page_Load (object sender, EventArgs e) {dt = new DataTable ();
DataColumn col = new DataColumn (); Col.
Columnname= "id"; Col.
DataType =system.type.gettype ("System.String"); Dt.
Columns.Add (COL);
col = new DataColumn (); Col.
Columnname= "Name"; Col.
DataType =system.type.gettype ("System.String"); Dt.
Columns.Add (COL);
col = new DataColumn (); Col.
Columnname= "Num"; Col.
DataType =system.type.gettype ("System.Int32"); Dt.
Columns.Add (COL);
col = new DataColumn (); Col.
Columnname= "Price"; Col.
DataType =system.type.gettype ("System.Single"); Dt.
Columns.Add (COL);
col = new DataColumn (); Col.
Columnname= "Total"; Col.
DataType =system.type.gettype ("System.Single"); Dt.
Columns.Add (COL); if (!
IsPostBack) {Bind (); } public void Bind () {if (session["shopcar"] = = null) {Response.Write ("<script>if (Confirm (' You are not logged in ') window.location= ' default15.aspx '; elseWindow.close ();</script> ");
else {ht = (Hashtable) session["Shopcar"]; foreach (object item in HT. Keys) {String id = Item.
ToString (); int num = Int. Parse (Ht[item].
ToString ());
String sql = "Selectbook_name,price from Book_info where book_id=" + ID + "'";
conn = new SqlConnection (connstring);
cmd = new SqlCommand (SQL, conn); Conn.
Open (); SDR =cmd.
ExecuteReader (); if (SDR). HasRows) {SDR.
Read (); DataRow row = dt.
NewRow ();
row["id" = ID;
row["num" = num; row["Name" = SDR.
GetString (0); row["Price"] =float. Parse (Sdr[1].
ToString ()); row["Total"] =num* (float. Parse (Sdr[1].
ToString ())); Dt.
Rows.Add (row); } SDR.
Close (); Conn.
Close (); } gridview1.datasource = dt.
DefaultView;
Gridview1.databind ();
}
}
D, you can see the items the user buys, but you cannot modify the quantity or delete it.
E, add modified quantity, delete merchandise feature, define columns in the GridView in the ASPX page:
<asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "False" > <Columns> <asp:bound Field datafield= "id" headertext= "id"/> <asp:boundfield datafield= "name" headertext= "names"/> <asp
: BoundField datafield= "Price" headertext= "Prices"/> <asp:TemplateField> <ItemTemplate> <asp:textbox runat= "Server" id= "TextBox1" Text= ' <%# Eval ("Num")%> ' ontextchanged= ' Textbox1_te Xtchanged "autopostback=" True "></asp:TextBox> </ItemTemplate> </asp:templatefield&
Gt
<asp:boundfield datafield= "Total" headertext= "totals"/> <asp:TemplateField> <ItemTemplate> <asp:button runat= "Server" id= "button1" Commandargument= ' <%# Eval ("ID")%> ' text= ' delete ' onclick= ' bu
Tton1_click "/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:Gridview>
F, add the TextChanged event for the text box in the GridView:
protected void textBox1_TextChanged (object sender, EventArgs e)
{
Hashtable ht = (Hashtable) session["Shopcar"] ;
if (HT = = null) return;
for (int i = 0; i < gridview1.rows.count;i++)
{
string ID =gridview1.rows[i]. Cells[0]. Text.tostring ();
Response.Write (ID);
String num = ((TextBox) gridview1.rows[i]. FindControl ("TextBox1")). Text;
Response.Write (" " +num+ "<br/>");
Ht[id] = num;
}
session["Shopcar"] = HT;
Bind ();
}
G, add a click event for the button:
protected void Button1_Click (object sender, EventArgs e)
{
String id = ((Button) sender). CommandArgument;
Hashtable ht = (Hashtable) session["Shopcar"];
if (HT = = null) return;
Ht. Remove (ID);
Bind ();
}
Shopping Cart code: Showcart.aspx.cs
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Collections;
Using System.Data;
Using System.Data.SqlClient;
Public partial class Shopcart:System.Web.UI.Page {Hashtable ht;
DataTable DT; String connstr = "Data source=.\\sqlexpress;" Attachdbfilename=f: \\test.mdf;integrated security=true; Connect timeout=30;
User Instance=true ";
SqlConnection Conn;
SqlCommand cmd;
SqlDataReader SDR;
protected void Page_Load (object sender, EventArgs e) {dt = new DataTable ();
DataColumn col = new DataColumn (); Col.
ColumnName = "id"; Col.
DataType = System.Type.GetType ("System.String"); Dt.
Columns.Add (COL);
col = new DataColumn (); Col.
ColumnName = "name"; Col.
DataType = System.Type.GetType ("System.String"); Dt.
Columns.Add (COL);
col = new DataColumn (); Col.
ColumnName = "Num"; Col. DataType = System.Type.GetTypE ("System.Int32"); Dt.
Columns.Add (COL);
col = new DataColumn (); Col.
ColumnName = "Price"; Col.
DataType = System.Type.GetType ("System.Single"); Dt.
Columns.Add (COL);
col = new DataColumn (); Col.
ColumnName = "Total"; Col.
DataType = System.Type.GetType ("System.Single"); Dt.
Columns.Add (COL); if (!
IsPostBack) {Bind (); } public void Bind () {if (session["shopcar"] = = null) {Response.Write ("<script>if" (
Confirm (' You are not logged in ') window.location= ' default.aspx '; else Window.close ();</script> ");
else {ht = (Hashtable) session["Shopcar"]; foreach (object item in HT. Keys) {String id = Item.
ToString (); int num = Int. Parse ((Ht[item).
ToString ()));
String sql = "Select Book_name,price from Book_info where book_id= '" + ID + "'";
conn = new SqlConnection (CONNSTR);
cmd = new SqlCommand (SQL, conn); Conn. Open ();
SDR = cmd.
ExecuteReader (); if (SDR). HasRows) {SDR.
Read (); DataRow row = dt.
NewRow ();
row["id" = ID;
row["num" = num; row["Name" = SDR.
GetString (0); row["Price" = float. Parse (Sdr[1].
ToString ()); row["Total" = num * (float. Parse (Sdr[1].
ToString ())); Dt.
Rows.Add (row); } SDR.
Close (); Conn.
Close (); } gridview1.datasource = dt.
DefaultView;
Gridview1.databind (); } protected void textBox1_TextChanged (object sender, EventArgs e) {Hashtable ht = (Hashtable) session["Shopcar"
];
if (HT = = null) return; for (int i = 0; i < GridView1.Rows.Count; i++) {string id = gridview1.rows[i]. Cells[0].
Text.tostring ();
Response.Write (ID); String num = ((TextBox) gridview1.rows[i]. FindControl ("TextBox1")).
Text;
Response.Write ("" + num + "<br/>");
Ht[id] = num; } session["Shopcar"] = HT;
Bind (); } protected void button1_click (object sender, EventArgs e) {String id = ((Button) sender).
CommandArgument;
Hashtable ht = (Hashtable) session["Shopcar"];
if (HT = = null) return; Ht.
Remove (ID);
Bind ();
}
}
Make a simple shopping cart is so simple, you can follow my ideas to create, on the basis of adding some features.