ShoppingCart.aspx.cs
资源名称:SHOPASP.rar [点击查看]
上传用户:xiecaij
上传日期:2015-02-08
资源大小:2016k
文件大小:5k
源码类别:
百货/超市行业
开发平台:
ASP/ASPX
- 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 eshop
- {
- /// <summary>
- /// ShoppingCart 的摘要说明。
- /// </summary>
- public class ShoppingCart : System.Web.UI.Page
- {
- protected System.Web.UI.WebControls.Label MyError;
- protected System.Web.UI.WebControls.DataGrid MyList;
- 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.HtmlControls.HtmlGenericControl LogOutArea;
- protected System.Web.UI.HtmlControls.HtmlGenericControl LogInArea;
- protected System.Web.UI.HtmlControls.HtmlForm Form1;
- private void Page_Load(object sender, System.EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- ShowShoppingCartList();
- ShowMenu();
- }
- }
- void ShowMenu()
- {
- if (Request.IsAuthenticated == true)
- {
- LogOutArea.Visible = false;
- LogInArea.Visible = true;
- }
- else
- {
- LogOutArea.Visible = true;
- LogInArea.Visible = false;
- }
- }
- //********************************************************
- //
- //ShowShoppingCartList()用来动态绑定用
- //户的购物车信息到DataGrid控件MyList
- //
- //********************************************************
- void ShowShoppingCartList()
- {
- BLL.ShoppingCart cart = new BLL.ShoppingCart();
- // 得到用户的购物车ID
- String cartID = cart.GetShoppingCartId();
- // 如果购物车内没有商品,DataGrid隐藏
- if (cart.GetItemCount(cartID) == 0)
- {
- DetailsPanel.Visible = false;
- MyError.Text = "购物车内没有商品。";
- }
- else
- {
- // 绑定购物车信息到DataGrid
- MyList.DataSource = cart.GetItems(cartID);
- MyList.DataBind();
- //显示总金额
- lblTotal.Text = String.Format( "{0:c}", cart.GetTotal(cartID));
- }
- }
- //*******************************************************
- //
- // UpdateShoppingCartDatabase方法根据客户端输入的更改信息
- // 提交数据库,更改购物车信息
- //
- //*******************************************************
- void UpdateShoppingCartDatabase()
- {
- BLL.ShoppingCart cart = new BLL.ShoppingCart();
- // 获得用户的cartID
- String cartID = cart.GetShoppingCartId();
- // 遍历DataGrid的每一行
- 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");
- // 出错处理。防止用户的非法输入,如quanlity为负数等
- int quantity;
- try
- {
- quantity = Int32.Parse(quantityTxt.Text);
- // 如果数量被修改或者Remove复选框被选中
- if (quantity != Convert.ToInt32(MyList.DataKeys[i]) || remove.Checked == true)
- {
- Label lblBookID = (Label) MyList.Items[i].FindControl("bookID");
- //数量为0或用户选择删除
- if (quantity == 0 || remove.Checked == true)
- {
- cart.RemoveItem(cartID, Int32.Parse(lblBookID.Text));
- }
- else
- {
- cart.UpdateItem(cartID, Int32.Parse(lblBookID.Text),quantity);
- }
- }
- }
- catch
- {
- MyError.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();
- ShowShoppingCartList();
- }
- private void CheckoutBtn_Click(object sender, System.EventArgs e)
- {
- //更新购物车记录
- UpdateShoppingCartDatabase();
- //如果购物车不为空,跳转到CheckOut页面
- //否则,给出错误提示信息
- BLL.ShoppingCart cart = new BLL.ShoppingCart();
- //获得购物车ID
- string cartID = cart.GetShoppingCartId();
- //检查购物记录是否为0
- if (cart.GetItemCount(cartID) != 0)
- {
- Response.Redirect("CheckOut.aspx");
- }
- else
- {
- MyError.Text = "购物车为空,不能提交!";
- }
- }
- }
- }