Background
Public partial class shopCart: System. Web. UI. Page
{
CommonClass ccObj = new CommonClass ();
DBClass dbObj = new DBClass ();
String strSql;
DataTable dtTable;
Hashtable hashCar;
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
/* Determine whether to log on */
ST_check_Login ();
If (Session ["ShopCart"] = null)
{
// If there is no shopping, the corresponding information is provided and the button is hidden
This. labMessage. Text = "You have not purchased! ";
This. labMessage. Visible = true; // displays the prompt information.
This. lnkbtnCheck. Visible = false; // hide the "go to service desk" button
This. lnkbtnClear. Visible = false; // hide the "clear shopping cart" button.
This. lnkbtnContinue. Visible = false; // hide the "continue shopping" button}
Else
{
HashCar = (Hashtable) Session ["ShopCart"]; // get its shopping cart
If (hashCar. Count = 0)
{
// If there is no shopping, the corresponding information is provided and the button is hidden
This. labMessage. Text = "You have no items in your shopping cart! ";
This. labMessage. Visible = true; // displays the prompt information.
This. lnkbtnCheck. Visible = false; // hide the "go to service desk" button
This. lnkbtnClear. Visible = false; // hide the "clear shopping cart" button.
This. lnkbtnContinue. Visible = false; // hide the "continue shopping" button}
Else
{
// Set the shopping cart content data source
DtTable = new DataTable ();
DataColumn column1 = new DataColumn ("No"); // Serial Number Column
DataColumn column2 = new DataColumn ("BookID"); // item ID
DataColumn column3 = new DataColumn ("BookName"); // Item Name
DataColumn column4 = new DataColumn ("Num"); // quantity
DataColumn column5 = new DataColumn ("price"); // unit price
DataColumn column6 = new DataColumn ("totalPrice"); // total price
DtTable. Columns. Add (column1); // Add a new column
DtTable. Columns. Add (column2 );
DtTable. Columns. Add (column3 );
DtTable. Columns. Add (column4 );
DtTable. Columns. Add (column5 );
DtTable. Columns. Add (column6 );
DataRow row;
// Traverse each row in the data table and assign values to the new columns of each row
Foreach (object key in hashCar. Keys)
{
Row = dtTable. NewRow ();
Row ["BookID"] = key. ToString ();
Row ["Num"] = hashCar [key]. ToString ();
DtTable. Rows. Add (row );
}
// Calculate the price
DataTable dstable;
Int I = 1;
Float price; // unit price
Int count; // number of items
Float totalPrice = 0; // total price of the product
Foreach (DataRow drRow in dtTable. Rows)
{
StrSql = "select BookName, HotPrice from tb_BookInfo where BookID =" + Convert. ToInt32 (drRow ["BookID"]. ToString ());
Dstable = dbObj. GetDataSetStr (strSql, "tbGI ");
DrRow ["No"] = I; // serial number
DrRow ["BookName"] = dstable. Rows [0] [0]. ToString (); // Item Name
DrRow ["price"] = (dstable. Rows [0] [1]. ToString (); // unit price
Price = float. Parse (dstable. Rows [0] [1]. ToString (); // unit price
Count = Int32.Parse (drRow ["Num"]. ToString ());
DrRow ["totalPrice"] = price * count; // total price
TotalPrice + = price * count; // calculate the combined price
I ++;
}
This. labTotalPrice. Text = "total price:" + totalPrice. ToString (); // display the prices of all products
This. gvShopCart. DataSource = dtTable. DefaultView; // bind the GridView control.
This. gvShopCart. DataKeyNames = new string [] {"BookID "};
This. gvShopCart. DataBind ();
}
}
}
}
Public void ST_check_Login ()
{
If (Session ["Username"] = null ))
{
Response. Write ("<script> alert ('Sorry! You are not a member. Please register first! '); Location = 'default. aspx' </script> ");
Response. End ();
}
}
Public void bind ()
{
If (Session ["ShopCart"] = null)
{
// If there is no shopping, the corresponding information is provided and the button is hidden
This. labMessage. Text = "You have not purchased! ";
This. labMessage. Visible = true; // displays the prompt information.
This. lnkbtnCheck. Visible = false; // hide the "go to service desk" button
This. lnkbtnClear. Visible = false; // hide the "clear shopping cart" button.
This. lnkbtnContinue. Visible = false; // hide the "continue shopping" button
}
Else
{
HashCar = (Hashtable) Session ["ShopCart"]; // get its shopping cart
If (hashCar. Count = 0)
{
// If there is no shopping, the corresponding information is provided and the button is hidden
This. labMessage. Text = "You have no items in your shopping cart! ";
This. labMessage. Visible = true; // displays the prompt information.
This. lnkbtnCheck. Visible = false; // hide the "go to service desk" button
This. lnkbtnClear. Visible = false; // hide the "clear shopping cart" button.
This. lnkbtnContinue. Visible = false; // hide the "continue shopping" button
}
Else
{
// Set the shopping cart content data source
DtTable = new DataTable ();
DataColumn column1 = new DataColumn ("No"); // Serial Number Column
DataColumn column2 = new DataColumn ("BookID"); // item ID
DataColumn column3 = new DataColumn ("BookName"); // Item Name
DataColumn column4 = new DataColumn ("Num"); // quantity
DataColumn column5 = new DataColumn ("price"); // unit price
DataColumn column6 = new DataColumn ("totalPrice"); // total price
DtTable. Columns. Add (column1); // Add a new column
DtTable. Columns. Add (column2 );
DtTable. Columns. Add (column3 );
DtTable. Columns. Add (column4 );
DtTable. Columns. Add (column5 );
DtTable. Columns. Add (column6 );
DataRow row;
// Traverse each row in the data table and assign values to the new columns of each row
Foreach (object key in hashCar. Keys)
{
Row = dtTable. NewRow ();
Row ["BookID"] = key. ToString ();
Row ["Num"] = hashCar [key]. ToString ();
DtTable. Rows. Add (row );
}
// Calculate the price
DataTable dstable;
Int I = 1;
Float price; // unit price
Int count; // number of items
Float totalPrice = 0; // total price of the product
Foreach (DataRow drRow in dtTable. Rows)
{
StrSql = "select BookName, HotPrice from tb_BookInfo where BookID =" + Convert. ToInt32 (drRow ["BookID"]. ToString ());
Dstable = dbObj. GetDataSetStr (strSql, "tbGI ");
DrRow ["No"] = I; // serial number
DrRow ["BookName"] = dstable. Rows [0] [0]. ToString (); // Item Name
DrRow ["price"] = (dstable. Rows [0] [1]. ToString (); // unit price
Price = float. Parse (dstable. Rows [0] [1]. ToString (); // unit price
Count = Int32.Parse (drRow ["Num"]. ToString ());
DrRow ["totalPrice"] = price * count; // total price
TotalPrice + = price * count; // calculate the combined price
I ++;
}
This. labTotalPrice. Text = "total price:" + totalPrice. ToString (); // display the prices of all products
This. gvShopCart. DataSource = dtTable. DefaultView; // bind the GridView control.
This. gvShopCart. DataKeyNames = new string [] {"BookID "};
This. gvShopCart. DataBind ();
}
}
}
Protected void lnkbtnUpdate_Click (object sender, EventArgs e)
{
HashCar = (Hashtable) Session ["ShopCart"]; // get its shopping cart
// Use the foreach statement to traverse and update the quantity of items in the shopping cart
Foreach (GridViewRow gvr in this. gvShopCart. Rows)
{
TextBox otb = (TextBox) gvr. FindControl ("txtNum"); // locate the TextBox Control used to enter the number
Int count = Int32.Parse (otb. Text); // obtain the number of user input values
String BookID = gvr. Cells [1]. Text; // obtain the ID of the item
HashCar [BookID] = count; // update the hashTable table
}
Session ["ShopCart"] = hashCar; // update the shopping cart
Response. Redirect ("shopCart. aspx ");
}
Protected void lnkbtnDelete_Command (object sender, CommandEventArgs e)
{
HashCar = (Hashtable) Session ["ShopCart"]; // get its shopping cart
// Remove the specified item from the Hashtable table from the shopping cart. The CommandArgument parameter value of the delete button (lnkbtnDelete) is the item ID.
HashCar. Remove (e. CommandArgument );
Session ["ShopCart"] = hashCar; // update the shopping cart
Response. Redirect ("shopCart. aspx ");
}
Protected void lnkbtnClear_Click (object sender, EventArgs e)
{
Session ["ShopCart"] = null;
Response. Redirect ("shopCart. aspx ");
}
Protected void lnkbtnContinue_Click (object sender, EventArgs e)
{
Response. Redirect ("Default. aspx ");
}
Protected void lnkbtnCheck_Click (object sender, EventArgs e)
{
Response. Redirect ("checkOut. aspx ");
}
Protected void gvShopCart_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
GvShopCart. PageIndex = e. NewPageIndex;
Bind ();
}
Protected void txtNum_TextChanged (object sender, EventArgs e)
{
HashCar = (Hashtable) Session ["ShopCart"]; // get its shopping cart
Foreach (GridViewRow gvr in this. gvShopCart. Rows)
{
TextBox otb = (TextBox) gvr. FindControl ("txtNum"); // locate the TextBox Control used to enter the number
Int count = Int32.Parse (otb. Text); // obtain the number of user input values
String BookID = gvr. Cells [1]. Text; // obtain the ID of the item
HashCar [BookID] = count; // update the hashTable table
}
Session ["ShopCart"] = hashCar; // update the shopping cart
Bind ();
}
}