该实例共有三个页面(投票页面,查看结果页面,投票管理页面),一个类(DB.cs)。
在DB中有GetCon(配置连接字符串并返回SqlConnection),ExSql(执行sql语句并返回Bool显示是否成功),reDs(查询数据库并返回DataSet)三个方法。
在购物车页面中有dlSoppingCart(显示商品DataList控件),lnkbtnClear(清空购物车Button控件),lnkbtnSettleAccounts(结账Button控件)。
cs代码:
Code
//存放购物车中商品总价格
public static string M_str_Count;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//向购物车中添加商品,如果购物车中已经存在该商品,则商品数量加1,如果是第一次购买,则向购物车中添加一条商品信息,
string P_str_CartID = Session["USerID"].ToString();
string P_ste_GoodID = Request["GoodsID"];
//从tb_Cart购物车信息表中查询该用户是否购买了该商品
DataSet ds = DB.reDs("select count(*) from tb_Cart where CartID=" + P_str_CartID + " and GoodsID=" + P_ste_GoodID);
if (ds.Tables[0].Rows[0][0].ToString() == "0")
{
//从tb_GoodsInfo产品信息表中查询出产品名称和价格
DataSet ds1 = DB.reDs("select GoodName,GoodsPrice from tb_GoodsInfo where GoodsID=" + P_ste_GoodID);
string P_str_GoodsName = ds1.Tables[0].Rows[0][0].ToString();
string P_str_GoodsPrice = ds1.Tables[0].Rows[0][1].ToString();
string P_str_Num = "1";
//将用户名,产品ID,产品名,产品价格,产品数量添加到tb_Cart
DB.ExSql("insert into tb_Cart values(" + P_str_CartID + "," + P_ste_GoodID + ",'" + P_str_GoodsName + "'," + P_str_GoodsPrice + "," + P_str_Num + ")");
}
else
{
//将商品数量+1
DB.ExSql("update tb_Cart set Num=Num+1 where CartID=" + P_str_CartID + " and GoodsID=" + P_ste_GoodID);
}
//显示购物车中的商品信息
Bind();
}
}
//绑定DataList控件
private void Bind()
{
DataSet ds2 = DB.reDs("select *,GoodsPrice*Num as Count from tb_Cart where CartID=" + Session["USerID"]);
float P_fl_Count = 0;
foreach (DataRow dr in ds2.Tables[0].rows)
{
P_fl_Count += Convert.ToSingle(dr[6]);
}
//购物车中商品总价格
M_str_Count = P_fl_Count.ToString();
dlSoppingCart.DataSource = ds2;
dlSoppingCart.DataBind();
}
protected void dlSoppingCart_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 tb_Cart where CartID=" + Session["USerID"]);
if (!P_bool_reVal)
{
Response.Write("<script>清空失败,请重试!</script>");
}
else
{
Bind();
}
}
//lnkbtnClear按钮Load事件,清空购物车时的提示信息
protected void lnkbtnClear_Load(object sender, EventArgs e)
{
lnkbtnClear.Attributes["onclick"] = "javascript:return confirm('你确定要清空购物车吗?')";
}
//DataList控件中的删除按钮
protected void dlSoppingCart_DeleteCommand(object source, DataListCommandEventArgs e)
{
bool P_bool_reVal = DB.ExSql("delete from tb_Cart where CartID=" + Session["UserID"] + " and GoodsID=" + e.CommandArgument.ToString());
if (!P_bool_reVal)
{
Response.Write("<script>清空失败,请重试!</script>");
}
else
{
Bind();
}
}
//lnkbtnDel按钮Load事件,删除购物车中商品时的提示信息
protected void lnkbtnClear_Load(object sender, EventArgs e)
{
lnkbtnClear.Attributes["onclick"] = "javascript:return confirm('你确定要清空购物车吗?')";
}
//更新购物车
protected void dlSoppingCart_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 tb_Cart set Num=" + P_str_Num + " where CartID=" + Session["USerID"]) + " and GoodsID=" + 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());
if (P_str_Money < Convert.ToDecimal(M_str_Count))
{
Response.Write("<script>alert('你的余额不足,请重新充值后再够买!')</script>");
}
else
{
//删除购物车表中该用户的信息
bool P_bool_reVal1 = DB.ExSql("delete from tb_Cart where CartID=" + Session["UserID"]);
//扣除用户金额
bool P_bool_reVal2 = DB.ExSql("update tb_User set Money=Money-"+M_str_Count+" where UserID=" + Session["UserID"].ToString());
if (!P_bool_reVal1 & !P_bool_reVal2)
{
Response.Write("<script>alert('结账失败,请重试!')</script>");
}
else
{
Bind();
Response.Write("<script>window.open('SuccessShop.aspx','','width=300px;height=250px;staus=no;help=no;scrollbars=no');</script>");
}
}
}
}
//存放购物车中商品总价格
public static string M_str_Count;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//向购物车中添加商品,如果购物车中已经存在该商品,则商品数量加1,如果是第一次购买,则向购物车中添加一条商品信息,
string P_str_CartID = Session["USerID"].ToString();
string P_ste_GoodID = Request["GoodsID"];
//从tb_Cart购物车信息表中查询该用户是否购买了该商品
DataSet ds = DB.reDs("select count(*) from tb_Cart where CartID=" + P_str_CartID + " and GoodsID=" + P_ste_GoodID);
if (ds.Tables[0].Rows[0][0].ToString() == "0")
{
//从tb_GoodsInfo产品信息表中查询出产品名称和价格
DataSet ds1 = DB.reDs("select GoodName,GoodsPrice from tb_GoodsInfo where GoodsID=" + P_ste_GoodID);
string P_str_GoodsName = ds1.Tables[0].Rows[0][0].ToString();
string P_str_GoodsPrice = ds1.Tables[0].Rows[0][1].ToString();
string P_str_Num = "1";
//将用户名,产品ID,产品名,产品价格,产品数量添加到tb_Cart
DB.ExSql("insert into tb_Cart values(" + P_str_CartID + "," + P_ste_GoodID + ",'" + P_str_GoodsName + "'," + P_str_GoodsPrice + "," + P_str_Num + ")");
}
else
{
//将商品数量+1
DB.ExSql("update tb_Cart set Num=Num+1 where CartID=" + P_str_CartID + " and GoodsID=" + P_ste_GoodID);
}
//显示购物车中的商品信息
Bind();
}
}
//绑定DataList控件
private void Bind()
{
DataSet ds2 = DB.reDs("select *,GoodsPrice*Num as Count from tb_Cart where CartID=" + Session["USerID"]);
float P_fl_Count = 0;
foreach (DataRow dr in ds2.Tables[0].rows)
{
P_fl_Count += Convert.ToSingle(dr[6]);
}
//购物车中商品总价格
M_str_Count = P_fl_Count.ToString();
dlSoppingCart.DataSource = ds2;
dlSoppingCart.DataBind();
}
protected void dlSoppingCart_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 tb_Cart where CartID=" + Session["USerID"]);
if (!P_bool_reVal)
{
Response.Write("<script>清空失败,请重试!</script>");
}
else
{
Bind();
}
}
//lnkbtnClear按钮Load事件,清空购物车时的提示信息
protected void lnkbtnClear_Load(object sender, EventArgs e)
{
lnkbtnClear.Attributes["onclick"] = "javascript:return confirm('你确定要清空购物车吗?')";
}
//DataList控件中的删除按钮
protected void dlSoppingCart_DeleteCommand(object source, DataListCommandEventArgs e)
{
bool P_bool_reVal = DB.ExSql("delete from tb_Cart where CartID=" + Session["UserID"] + " and GoodsID=" + e.CommandArgument.ToString());
if (!P_bool_reVal)
{
Response.Write("<script>清空失败,请重试!</script>");
}
else
{
Bind();
}
}
//lnkbtnDel按钮Load事件,删除购物车中商品时的提示信息
protected void lnkbtnClear_Load(object sender, EventArgs e)
{
lnkbtnClear.Attributes["onclick"] = "javascript:return confirm('你确定要清空购物车吗?')";
}
//更新购物车
protected void dlSoppingCart_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 tb_Cart set Num=" + P_str_Num + " where CartID=" + Session["USerID"]) + " and GoodsID=" + 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());
if (P_str_Money < Convert.ToDecimal(M_str_Count))
{
Response.Write("<script>alert('你的余额不足,请重新充值后再够买!')</script>");
}
else
{
//删除购物车表中该用户的信息
bool P_bool_reVal1 = DB.ExSql("delete from tb_Cart where CartID=" + Session["UserID"]);
//扣除用户金额
bool P_bool_reVal2 = DB.ExSql("update tb_User set Money=Money-"+M_str_Count+" where UserID=" + Session["UserID"].ToString());
if (!P_bool_reVal1 & !P_bool_reVal2)
{
Response.Write("<script>alert('结账失败,请重试!')</script>");
}
else
{
Bind();
Response.Write("<script>window.open('SuccessShop.aspx','','width=300px;height=250px;staus=no;help=no;scrollbars=no');</script>");
}
}
}
}