📄 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;
namespace MobileOnlineShop
{
/// <summary>
/// ShoppingCart 的摘要说明。
/// </summary>
public class ShoppingCart : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyList;
protected System.Web.UI.WebControls.Label lblTotal;
protected System.Web.UI.WebControls.ImageButton UpdateBtn;
protected System.Web.UI.WebControls.ImageButton ImageButton1;
protected System.Web.UI.WebControls.Panel DetailsPanel;
protected System.Web.UI.HtmlControls.HtmlTable Table1;
protected System.Web.UI.HtmlControls.HtmlTable Table4;
protected System.Web.UI.WebControls.Label MyError;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
private void Page_Load(object sender, System.EventArgs e)
{
if(this.IsPostBack == false)
{
ShowShoppingCartList();
}
}
#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.Web.UI.ImageClickEventHandler(this.UpdateBtn_Click);
this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void UpdateBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
UpdateShoppingCartDatabase();
ShowShoppingCartList();
}
private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
//先更新ShoppingCart表
UpdateShoppingCartDatabase();
//实例化购物车模块数据访问类ShoppingCartDB为对象cart
MobileOnlineShop.DAL.ShoppingCartDB cart = new MobileOnlineShop.DAL.ShoppingCartDB();
//
string cartID=cart.GetShoppingCartID();
//
if(cart.GetItemCount(cartID) !=0)
{
//
Response.Redirect("Checkout.aspx");
}
else
{
//
this.MyError.Text="购物车为空不能进入结算中心页面";
}
}
/// <summary>
/// ShowShoppingCartList()显示客户购物车商品信息
/// </summary>
void ShowShoppingCartList()
{
//实例化购物车模块数据访问类ShoppingCartDB为对象cart
MobileOnlineShop.DAL.ShoppingCartDB cart=new MobileOnlineShop.DAL.ShoppingCartDB();
//获取当前用户的购物车ID
string cartID=cart.GetShoppingCartID();
// string cartID="1";
//如果此用户的购物车内没有商品,就隐藏购物车商品列表,并显示相关信息
if(cart.GetItemCount(cartID) ==0)
{
this.DetailsPanel.Visible=false;
this.MyError.Text="您的购物车内没有商品";
}
else
{
//指定DataGrid的数据源并绑定
this.MyList.DataSource = cart.GetItems(cartID);
this.MyList.DataBind();
//获取并显示购物车内商品的总额
this.lblTotal.Text = String.Format("{0:c}",cart.GetTotal(cartID));
}
}
/// <summary>
/// UpdateShoppingCartDatabase()更新当前购物车内详细购物信息
/// </summary>
void UpdateShoppingCartDatabase()
{
//实例化购物车模块数据访问类ShoppingCartDB为对象cart
MobileOnlineShop.DAL.ShoppingCartDB cart=new MobileOnlineShop.DAL.ShoppingCartDB();
// 获取当前用户的购物车标识
String cartId = cart.GetShoppingCartID();
// cartId="1";
// 遍历购物车列表中的每一条购物信息
for (int i=0; i < MyList.Items.Count; i++)
{
// 获取当前行的数据控件
TextBox quantityTxt = (TextBox) MyList.Items[i].FindControl("Quantity");
CheckBox remove = (CheckBox) MyList.Items[i].FindControl("Remove");
// 使用try/catch块来捕捉可能出现的错误
// 定义int型变量quantity
int quantity;
try
{
quantity = Int32.Parse(quantityTxt.Text);
// 判断订购商品数量是否更改和remove是否有效
if (quantity != (int)MyList.DataKeys[i] || remove.Checked == true)
{
Label lblProductID = (Label) MyList.Items[i].FindControl("ProductID");
//如果订购数量为0或用户选中了删除此商品,就删除此商品记录
if (quantity == 0 || remove.Checked == true)
{
cart.RemoveItem(cartId, Int32.Parse(lblProductID.Text));
}
//不然就更新ShoppingCart表
else
{
cart.UpdateItem(cartId, Int32.Parse(lblProductID.Text),quantity);
}
}
}
//若捕捉到任何错误就显示异常
catch
{
MyError.Text = "您的一个或多个输入不正确";
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -