?? 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 bookshop
{
/// <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.HtmlForm Form1;
public shoppingcart()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
if (Page.IsPostBack == false)
{
PopulateShoppingCartList();
}
}
#region Web 窗體設計器生成的代碼
/// <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();
}
private void CheckoutBtn_Click(object sender, System.EventArgs e)
{
UpdateShoppingCartDatabase();
bookshop.BookDBO cart = new bookshop.BookDBO();
String cartId = cart.GetShoppingCartId();
if (cart.CountShoppingCartItem(cartId) !=0)
{
Response.Redirect("Checkout.aspx");
}
else
{
MyError.Text = "您沒有購買商品,不能進入結賬頁面。";
}
}
void PopulateShoppingCartList()
{
bookshop.BookDBO cart = new bookshop.BookDBO();
String cartId = cart.GetShoppingCartId();
if (cart.CountShoppingCartItem(cartId) == 0)
{
DetailsPanel.Visible = false;
MyError.Text = "暫時為空,請購物。";
}
else
{
MyList.DataSource = cart.DisplayShoppingCart(cartId);
MyList.DataBind();
lblTotal.Text = String.Format( "{0:c}", cart.ShoppingCartTotalCost(cartId));
}
}
void UpdateShoppingCartDatabase()
{
bookshop.BookDBO cart = new bookshop.BookDBO();
String cartId = cart.GetShoppingCartId();
for (int i=0; i < MyList.Items.Count; i++)
{
//根據控件標識符查找控件,然后構造新的TextBox控件
TextBox quantityTxt = (TextBox) MyList.Items[i].FindControl("bookQuantity");
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("bookid");
if (quantity == 0 || remove.Checked == true)
{
cart.ShoppingCartRemoveItem(cartId, Int32.Parse(lblProductID.Text));
}
else
{
cart.ShoppingCartUpdate(cartId, Int32.Parse(lblProductID.Text), quantity);
}
}
}
catch
{
//出現異常顯示錯誤信息
MyError.Text = "您的輸入有問題。";
}
}
}
private void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -