?? 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;
using shop.DbBase;
using System.Text;
//該源碼下載自www.51aspx.com(51aspx.com)
namespace shop
{
/// <summary>
/// shoppingCart 的摘要說明。
/// </summary>
public class shoppingCart : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid myGrid;
protected System.Web.UI.WebControls.ImageButton imgUpdateCart;
protected System.Web.UI.WebControls.Label lblSum;
protected System.Web.UI.WebControls.ImageButton imgBtnClear;
#region 自定義私有變量
private double dSum;
//private string sProductId;
#endregion
private void Page_Load(object sender, System.EventArgs e)
{
// 獲得商品的信息
UpdateCart();
}
private void UpdateCart()
{
#region Version 1.0
/*
dSum = 0;
Hashtable oHT = (Hashtable)Session["ProductInCart"];
IDictionaryEnumerator oIDE = oHT.GetEnumerator();
Hashtable oHTNum = new Hashtable();
StringBuilder sbSql = new StringBuilder();
sbSql.Append("Select ProductId,PName,PMemberPrice,1 from product where ProductId in (");
while(oIDE.MoveNext())
{
sbSql.Append(oIDE.Key.ToString()+",");
oHTNum.Add(oIDE.Key.ToString(),oIDE.Value.ToString());
}
sbSql.Remove(sbSql.Length - 1,1);
sbSql.Append(")");
DataSet oDS = OleBase.ExecuteSql4Ds(sbSql.ToString());
foreach(DataRow oDR in oDS.Tables[0].Rows)
{
oDR.ItemArray[3] = oHTNum[oDR.ItemArray[0]].ToString();
dSum += double.Parse(oDR.ItemArray[2].ToString())*int.Parse(oDR.ItemArray[3].ToString());
}
*/
#endregion
//新建數(shù)據(jù)表
DataTable oDT = new DataTable();
oDT.Columns.Add("ProductId");
oDT.Columns.Add("PName");
oDT.Columns.Add("PMemberPrice");
oDT.Columns.Add("PNumber");
dSum = 0;
//判斷購物車中是否存在數(shù)據(jù)
if(!object.Equals(Session["ProductInCart"],null))
{
IDictionaryEnumerator oIDE = oHT.GetEnumerator();
DataRow oDR;
while(oIDE.MoveNext())
{
oDR = oDT.NewRow();
//定義一個sql查詢語句
string sSql = "Select ProductId,PName,PMemberPrice from product where ProductId=" + oIDE.Key.ToString();
//調(diào)用類中的方法
DataSet oDS = OleBase.ExecuteSql4Ds(sSql);
oDR["ProductId"] = oDS.Tables[0].Rows[0].ItemArray[0].ToString();
oDR["PName"] = oDS.Tables[0].Rows[0].ItemArray[1].ToString();
oDR["PMemberPrice"] = oDS.Tables[0].Rows[0].ItemArray[2].ToString();
oDR["PNumber"] = oIDE.Value.ToString();
//獲得商品的總額
dSum += double.Parse(oDR["PMemberPrice"].ToString())*int.Parse(oDR["PNumber"].ToString());
oDT.Rows.Add(oDR);
}
}
//設(shè)置myGrid的數(shù)據(jù)源
myGrid.DataSource = oDT.DefaultView;
//綁定myGrid
myGrid.DataBind();
//將商品的總額綁定到lblSum控件上
lblSum.Text = dSum.ToString();
}
private void imgUpdateCart_Click(object sender,ImageClickEventArgs e)
{
Label oLblProductId;
TextBox oTxtNumber;
CheckBox oChkRemove;
//判斷購物中是否存在數(shù)據(jù)
if(!object.Equals(Session["ProductInCart"],null))
Session.Remove("ProductInCart");
Session["ProductInCart"] = new Hashtable();
for(int i=0;i<myGrid.Items.Count;i++)
{
oLblProductId = (Label)myGrid.Items[i].FindControl("lblProductId");
oTxtNumber = (TextBox)myGrid.Items[i].FindControl("txtNumber");
oChkRemove = (CheckBox)myGrid.Items[i].FindControl("chkRemove");
if(!oChkRemove.Checked)
((Hashtable)Session["ProductInCart"]).Add(oLblProductId.Text,oTxtNumber.Text);
}
//重新綁定
UpdateCart();
}
private void imgBtnClear_Click(object sender,ImageClickEventArgs e)
{
Session.Remove("ProductInCart");
UpdateCart();
}
#region Web 窗體設(shè)計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.imgUpdateCart.Click += new System.Web.UI.ImageClickEventHandler(this.imgUpdateCart_Click);
this.imgBtnClear.Click += new System.Web.UI.ImageClickEventHandler(this.imgBtnClear_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -