?? shoppingcartcontrol.ascx.cs
字號(hào):
?using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class controls_ShoppingCartControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 在頁(yè)面呈現(xiàn)前,先綁定用戶的購(gòu)物籃數(shù)據(jù)
/// </summary>
protected void Page_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCart();
}
}
/// <summary>
/// 將數(shù)據(jù)綁定到Repeater控件內(nèi)
/// </summary>
private void BindCart()
{
ICollection<CartItemInfo> cart = Profile.ShoppingCart.CartItems;
if (cart.Count > 0)
{
repShoppingCart.DataSource = cart;
repShoppingCart.DataBind();
PrintTotal();
plhTotal.Visible = true;
}
else
{
repShoppingCart.Visible = false;
plhTotal.Visible = false;
lblMsg.Text = "購(gòu)物籃中無(wú)任何圖書.";
}
}
/// <summary>
/// 重新計(jì)算總價(jià)格
/// </summary>
private void PrintTotal()
{
if (Profile.ShoppingCart.CartItems.Count > 0)
ltlTotal.Text = Profile.ShoppingCart.Total.ToString("c");
}
/// <summary>
/// 計(jì)算總價(jià)格
/// </summary>
protected void BtnTotal_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
TextBox txtQuantity;
ImageButton btnDelete;
int qty = 0;
foreach (RepeaterItem row in repShoppingCart.Items)
{
//定位到填寫數(shù)量的控件
txtQuantity = (TextBox)row.FindControl("txtQuantity");
btnDelete = (ImageButton)row.FindControl("btnDelete");
//驗(yàn)證用戶的輸入數(shù)據(jù)
if (int.TryParse(Product.InputText(txtQuantity.Text, 10), out qty))
{
//判斷數(shù)量的值
if (qty > 0)
Profile.ShoppingCart.SetQuantity(int.Parse(btnDelete.CommandArgument), qty);
else if (qty == 0)
//如果值為0,則自動(dòng)清除此商品
Profile.ShoppingCart.Remove(int.Parse(btnDelete.CommandArgument));
}
}
Profile.Save();
BindCart();
}
/// <summary>
/// 處理刪除商品的方法
/// </summary>
protected void CartItem_Command(object sender, CommandEventArgs e)
{
//判斷選擇的按鈕屬性
switch (e.CommandName.ToString())
{
case "Del":
//如果是刪除,則執(zhí)行Remove方法
Profile.ShoppingCart.Remove(int.Parse(e.CommandArgument.ToString()));
break;
case "Move":
Profile.ShoppingCart.Remove(int.Parse(e.CommandArgument.ToString()));
break;
}
//保存Profile所做的更改
Profile.Save();
//重新綁定數(shù)據(jù)源。
BindCart();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -