📄 shoppingcart.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;
using System.Configuration;
using System.Data.SqlClient;
namespace StoreOnline
{
/// <summary>
/// ShoppingCart 的摘要说明。
/// </summary>
public class ShoppingCart : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label top;
protected System.Web.UI.WebControls.Label lblTotal;
protected System.Web.UI.WebControls.Button UpdateBtn;
protected System.Web.UI.WebControls.Button CheckoutBtn;
protected System.Web.UI.WebControls.Panel DetailsPanel;
protected System.Web.UI.WebControls.DataGrid MyList;
private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack == false)
{
PopulateShoppingCartList();
}
}
private void CheckoutBtn_Click(object sender, System.EventArgs e)
{
UpdateShoppingCartDatabase();
StoreOnline.StoreDBO cart = new StoreOnline.StoreDBO();
String cartId = cart.GetShoppingCartId();
if (cart.CountShoppingCartItem(cartId) !=0)
{
Response.Redirect("Checkout.aspx");
}
else
{
top.Text = "您没有购买商品,不能进入结账页面。";
}
}
void PopulateShoppingCartList()
{
StoreOnline.StoreDBO cart = new StoreOnline.StoreDBO();
String cartId = cart.GetShoppingCartId();
if (cart.CountShoppingCartItem(cartId) == 0)
{
DetailsPanel.Visible = false;
top.Text = "暂时为空,请购物。";
}
else
{
MyList.DataSource = cart.DisplayShoppingCart(cartId);
MyList.DataBind();
lblTotal.Text = String.Format( "{0:c}", cart.ShoppingCartTotalCost(cartId));
}
}
void UpdateShoppingCartDatabase()
{
StoreOnline.StoreDBO cart = new StoreOnline.StoreDBO();
String cartId = cart.GetShoppingCartId();
for (int i=0; i < MyList.Items.Count; i++)
{
//根据控件标识符查找控件,然后构造新的TextBox控件
TextBox quantityTxt = (TextBox) MyList.Items[i].FindControl("ProQuantity");
CheckBox remove = (CheckBox) MyList.Items[i].FindControl("Remove");
int quantity;
try
{
quantity = Int32.Parse(quantityTxt.Text);
//如果某种商品的数量改变了或者删除该商品的Check控件被选择了,那么调用相应的方法更新数据库
if (quantity != (int)MyList.DataKeys[i] || remove.Checked == true)
{
Label lblProductID = (Label) MyList.Items[i].FindControl("ProID");
if (quantity == 0 || remove.Checked == true)
{
cart.ShoppingCartRemoveItem(cartId, Int32.Parse(lblProductID.Text));
}
else
{
cart.ShoppingCartUpdate(cartId, Int32.Parse(lblProductID.Text), quantity);
}
}
}
catch
{
//出现异常显示错误信息
top.Text = "您的输入有问题。";
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.UpdateBtn.Click += new System.EventHandler(this.UpdateBtn_Click);
this.CheckoutBtn.Click += new System.EventHandler(this.CheckoutBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void UpdateBtn_Click(object sender, System.EventArgs e)
{
UpdateShoppingCartDatabase();
PopulateShoppingCartList();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -