shoppingcart.ascx.cs

来自「ASP.NET2.0(C#篇)经典教程的源码...本源码很好的实现了购物车...」· CS 代码 · 共 71 行

CS
71
字号
using System;
using System.Web.UI.WebControls;
using Wrox.Commerce;

partial class ShoppingCartControl : System.Web.UI.UserControl
{
  void Page_Load( object sender,  System.EventArgs e)
  {
    if (Profile.Cart == null)
      return;

    if (!Page.IsPostBack)
      BindGrid();

    if (Profile.Cart.Items.Count == 0)
    {
      TotalLabel.Visible = false;
      DiscountPanel.Visible = false;
    }
  }

  private void BindGrid()
  {
    // bind to the items in the cart
    CartGrid.DataSource = Profile.Cart.Items;
    DataBind();

    // TODO: sort out alignment
    if (Context.User.IsInRole("FanClubMember"))
    {
      SubTotalLabel.Text = string.Format("Sub-Total:{0,35:C}", Profile.Cart.SubTotal);
      MemberDiscount.Text = string.Format("Member Discount:{0:C}", Profile.Cart.MemberDiscount);
      DiscountPanel.Visible = true;
    }

    TotalLabel.Text = string.Format("Total:{0,19:C}", Profile.Cart.Total);
  }

  protected void CartGrid_RowEditing( object sender,  System.Web.UI.WebControls.GridViewEditEventArgs e)
  {
    CartGrid.EditIndex = e.NewEditIndex;
    BindGrid();
  }

  protected void CartGrid_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
  {
    TextBox QuantityTextBox = (TextBox)CartGrid.Rows[e.RowIndex].Cells[2].Controls[0];
    int Quantity = Convert.ToInt32(QuantityTextBox.Text);

    if (Quantity == 0)
      Profile.Cart.Items.RemoveAt(e.RowIndex);
    else
      Profile.Cart.Items[e.RowIndex].Quantity = Quantity;

    CartGrid.EditIndex = -1;
    BindGrid();
  }

  protected void CartGrid_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
  {
    CartGrid.EditIndex = -1;
    BindGrid();
  }

  protected void CartGrid_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
  {
    Profile.Cart.Items.RemoveAt(e.RowIndex);
    BindGrid();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?