📄 addtocart.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace BookStore
{
/// <summary>
/// AddToCart 的摘要说明。
/// </summary>
public class AddToCart : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lbInfo;
protected System.Web.UI.HtmlControls.HtmlForm QuickSearchFrom;
protected System.Web.UI.WebControls.Label lbAllPrice;
protected System.Web.UI.WebControls.Label lbAllDiscountPrice;
protected System.Web.UI.WebControls.Label lbDiscount;
protected System.Web.UI.WebControls.Button btToOrder;
protected System.Web.UI.WebControls.Button btClearAll;
protected System.Web.UI.WebControls.DataList dlCart;
static int nBookID, nUserID;
protected System.Web.UI.WebControls.Button btGoOn;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
if(Object.Equals(Session["UserName"],null))
{
Session["ReqestedURL"] = Request.RawUrl;
Response.Redirect("../Error.aspx");
}
else
{
nUserID = int.Parse(Session["UserID"].ToString());
if(Object.Equals(Request["BookID"],null))
{
BindData();
}
else
{
nBookID = int.Parse(Request["BookID"].ToString());
AddDataToCart();
BindData();
}
}
}
}
private void AddDataToCart()
{
string strSql;
// First, query if the book already in the shopping cart
strSql = "select Quantity from ShoppingCart where BookID=" + nBookID + " and UserID=" + nUserID;
// If has, give the information, go on databind
if(IsExist(strSql))
{
this.lbInfo.Text = "这本书已经在购物车中了,请直接更改这本书的购买数目。";
}
else
{
// If haven't, add to the database
strSql = "Insert into [ShoppingCart] (UserID,BookID,Quantity) Values("
+ nUserID + ","
+ nBookID + ","
+ 1 + ")";
try
{
RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);
}
catch
{
this.lbInfo.Text = "加入购物车失败!";
return;
}
}
}
private void BindData()
{
float fAllPrice, fAllDiscountPrice;
fAllPrice = 0;
fAllDiscountPrice = 0;
// Now, query the shoppingcart detail view, bind data to datalist
DataTable currentDT;
RobertSoft.BookStore.OrderBook currentOrder = new RobertSoft.BookStore.OrderBook();
currentDT = currentOrder.OrderDetail(nUserID);
DataRow currentDR;
for(int i=0; i < currentDT.Rows.Count; i++)
{
currentDR = currentDT.Rows[i];
fAllPrice += float.Parse(currentDR[4].ToString());
fAllDiscountPrice += float.Parse(currentDR[5].ToString());
}
this.dlCart.DataSource = currentDT.DefaultView;
this.dlCart.DataBind();
this.lbAllPrice.Text = fAllPrice.ToString();
this.lbAllDiscountPrice.Text = fAllDiscountPrice.ToString();
float fDiscount = fAllPrice - fAllDiscountPrice;
int nDiscount = (int) (fDiscount * 10);
fDiscount = (float)nDiscount / 10;
this.lbDiscount.Text = fDiscount.ToString();
}
private bool IsExist(string str)
{
try
{
RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLForValue(str);
return true;
}
catch
{
return false;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dlCart.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlCart_ItemCommand);
this.dlCart.EditCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlCart_EditCommand);
this.dlCart.DeleteCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlCart_DeleteCommand);
this.btToOrder.Click += new System.EventHandler(this.btToOrder_Click);
this.btClearAll.Click += new System.EventHandler(this.btClearAll_Click);
this.btGoOn.Click += new System.EventHandler(this.btGoOn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dlCart_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if(e.CommandName == "BookNameClick")
{
string url;
url = "../ShowBookDetail.aspx?ID=" + e.CommandArgument.ToString();
Response.Redirect(url);
}
}
private void dlCart_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
int intBookID = int.Parse(e.CommandArgument.ToString());
DeleteData(intBookID);
BindData();
}
private void dlCart_EditCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
int intBookID = int.Parse(e.CommandArgument.ToString());
string number = ((TextBox)(e.Item.Controls[7])).Text.Trim();
int n = int.Parse(number);
UpdateData(intBookID,n);
BindData();
}
private void UpdateData(int intBookID, int intQuantity)
{
string strSql = "Update [ShoppingCart] set Quantity="
+ intQuantity + " where BookID=" + intBookID + " and UserID=" + nUserID;
try
{
RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);
this.lbInfo.Text = "修改成功";
}
catch
{
this.lbInfo.Text = "更新失败!";
return;
}
}
private void DeleteData(int intBookID)
{
string strSql = "DELETE FROM [ShoppingCart] where BookID=" + intBookID + " and UserID=" + nUserID;
try
{
RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);
this.lbInfo.Text = "删除成功";
}
catch
{
this.lbInfo.Text = "删除失败!";
return;
}
}
private void btToOrder_Click(object sender, System.EventArgs e)
{
if(this.dlCart.Items.Count != 0)
{
string strUrl;
strUrl = "CartToOrder.aspx?UserID=" + nUserID;
Response.Redirect(strUrl);
}
else
{
this.lbInfo.Text = "您的购物车中还没有物品!";
this.lbInfo.ForeColor = Color.Red;
}
}
private void btClearAll_Click(object sender, System.EventArgs e)
{
if(this.dlCart.Items.Count != 0)
{
string strSql = "DELETE FROM [ShoppingCart] where UserID=" + nUserID;
try
{
RobertSoft.BookStore.DBClass.DBBaseClass.ExecuteSQLCmd(strSql);
BindData();
this.lbInfo.Text = "购物车清空成功";
}
catch
{
this.lbInfo.Text = "购物车清空失败!";
return;
}
}
else
{
this.lbInfo.Text = "您的购物车中还没有物品!";
this.lbInfo.ForeColor = Color.Red;
}
}
private void btGoOn_Click(object sender, System.EventArgs e)
{
Response.Redirect("../Default.aspx");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -