using System;
using System.Data;
using System.Configuration;
using
System.Collections;
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 ShoppingCart : System.Web.UI.Page
{
public
static string M_str_Count;
public string st;
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string P_str_UserName = "";
//向购物车中添加商品,如果购物车中已经存在该商品,则商品数量加1,如果是第一次购买,则向购物车中添加一条商品信息
try
{
P_str_UserName = Session["username"].ToString();
string P_str_BookId = Request["BookId"];
DataSet ds = DB.reDs("select count(*) from Cart where UserName=" +
P_str_UserName + "and BookId=" + P_str_BookId);
if
(ds.Tables[0].Rows[0][0].ToString() == "0")
{
DataSet ds1 = DB.reDs("select ItemName,bookPrice from
Item where BookId=" + P_str_BookId);
string
P_str_ItemName = ds1.Tables[0].Rows[0][0].ToString();
string P_str_Price = ds1.Tables[0].Rows[0][1].ToString();
string P_str_Quantity = "1";
DB.ExSql("insert into Cart
values(" + P_str_UserName + "," + P_str_BookId + ",‘" + P_str_ItemName + "‘," +
P_str_Price + "," + P_str_Quantity + ")");
}
else
{
DB.ExSql("update Cart set
Quantity=Quantity+1 where UserName=" + P_str_UserName + "and BookId=" +
P_str_BookId);
}
}
catch {
}
//显示购物车中的商品信息
Bind();
}
}
//绑定DataList控件
public void Bind()
{
string session="";
try
{
session = Session["username"].ToString
();
}
catch {
}
if
(!string.IsNullOrEmpty(session))
{
DataSet ds2
= DB.reDs("select *,Price*Quantity As Count from Cart where UserName=" +
Session["username"]);
float P_fl_Count = 0;
foreach (DataRow dr in ds2.Tables[0].Rows)
{
P_fl_Count += Convert.ToSingle(dr[5]);
}
M_str_Count = P_fl_Count.ToString();
dlShoppingCart.DataSource = ds2;
dlShoppingCart.DataBind();
}
else {
Response.Write(@"<script type=‘text/javascript‘>
alert
(‘请登陆!‘);location=‘userLogin.aspx‘; </script>");
}
}
protected void dlShoppingCart_ItemDataBound(object sender,
DataListItemEventArgs e)
{
//用来实现数量文本框中只能输入数字
TextBox
txtGoodsNum = (TextBox)e.Item.FindControl("txtGoodsNum");
if
(txtGoodsNum != null)
{
txtGoodsNum.Attributes["onkeyup"] = "value=value.replace(/[^\\d]/g,‘‘)";
}
}
//清空购物车
protected void lnkbtnClear_Click(object sender,
EventArgs e)
{
bool P_bool_reVal = DB.ExSql("Delete from Cart
where UserName=" + Session["username"]);
if (!P_bool_reVal)
Response.Write("<script>清空失败,请重试!</script>");
else
Bind();
}
//清空购物车时的提示信息
protected void
lnkbtnClear_Load(object sender, EventArgs e)
{
lnkbtnClear.Attributes["onclick"] = "javascript:return
confirm(‘你确定要清空购物车吗?‘)";
}
//继续购物
protected void lnkbtnContinue_Click(object sender, EventArgs
e)
{
Response.Redirect("~/Default.aspx");
}
//删除购物车中的商品
protected void dlShoppingCart_DeleteCommand(object source,
DataListCommandEventArgs e)
{
bool P_bool_reVal =
DB.ExSql("delete from Cart where UserName=" + Session["userName"] + " and
BookId=" + e.CommandArgument.ToString());
if (!P_bool_reVal)
Response.Write("<script>删除失败,请重试!</script>");
else
Bind();
}
//删除购物车中的商品时的提示信息
protected void lnkbtnDel_Load(object sender,
EventArgs e)
{
((LinkButton)sender).Attributes["onclick"] =
"javascript:return confirm(‘你确定要删除该物品吗?‘)";
}
//更新购物车
protected
void dlShoppingCart_ItemCommand(object source, DataListCommandEventArgs e)
{
//if (e.CommandName == "updateNum")
//{
//
string P_str_Num = ((TextBox)e.Item.FindControl("txtGoodsNum")).Text;
// bool P_bool_reVal = DB.ExSql("update Cart set Quantity=" + P_str_Num +
"where UserName=" + Session["userName"] + " and BookId=" +
e.CommandArgument.ToString());
// if (P_bool_reVal)
//
Bind();
//}
if (e.CommandName == "updateNum")
{
string P_str_Num = ((TextBox)e.Item.FindControl("txtGoodsNum")).Text;
bool P_bool_reVal = DB.ExSql("update Cart set Quantity=Quantity+1 where
UserName=" + Session["userName"] + " and BookId=" +
e.CommandArgument.ToString());
if (P_bool_reVal)
Bind();
}
if (e.CommandName == "updateNum1")
{
string
P_str_Num = ((TextBox)e.Item.FindControl("txtGoodsNum")).Text;
bool P_bool_reVal = DB.ExSql("update Cart set Quantity=Quantity-1 where
UserName=" + Session["userName"] + " and BookId=" +
e.CommandArgument.ToString());
if (P_bool_reVal)
Bind();
}
}
protected void lnkbtnSettleAccounts_Click(object sender, EventArgs e)
{
if (M_str_Count == "")
{
Response.Write("<script>alert(‘您的购物车中没有任何物品!‘);</script>");
}
else
{
DataSet ds = DB.reDs("select
Money from tb_User where UserID=" + Session["UserID"].ToString());
decimal P_str_Money =
Convert.ToDecimal(ds.Tables[0].Rows[0][0].ToString());
}
}
}
shoppingCart,码迷,mamicode.com