ASP .NET 一、纯asp.net+cookie制作的购物车
缺点,即使使用全局不缓存,仍然后以使用IE中的返回按钮
(1)添加Global.asax,在其中加入以下内容,设置全局不缓存
?protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetNoStore();
}
(2)制作产品销售页
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="4" AlternatingItemStyle-Wrap="true" ItemStyle-Width="100">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"product_pic") %>' /><br>
<asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"product_name") %>'></asp:Label><br>
<asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"product_price") %>'></asp:Label><br>
<asp:Label ID="Label3" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem,"product_id") %>'></asp:Label><br>
<asp:TextBox ID="TextBox1" runat="server" Width='35' MaxLength="3" Text="1">1</asp:TextBox>件
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="Button1_Command" CommandName='<%# DataBinder.Eval(Container.DataItem,"product_id") %>'>购买</asp:LinkButton>
</ItemTemplate>
</asp:DataList></div>
<asp:Button ID="Button1" runat="server" Text="清空购物车" οnclick="Button1_Click" />
</form>
<p><a href="shopcar.aspx">我的购物车(最多20条数据)</a></p>
</body>
</html>
为产品销售页添加代码:
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 Default8 : System.Web.UI.Page
{
database db = new database();
HttpCookie cok = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DataList1.DataSource = db.select_db("select * from product_list");
this.DataList1.DataBind(); //Convert.ToString();
}
}
protected void Button1_Command(object sender, CommandEventArgs e)
{
//标识产品的数量
int num = 1;
LinkButton lb = (LinkButton)sender;
string str = lb.ClientID;
string[] str2 = str.Split('_');
str2[1] = str2[1].Substring(3, str2[1].Length - 3);
int i = int.Parse(str2[1]);
if (((TextBox)this.DataList1.Items[i].FindControl("TextBox1")).Text.Trim().Length != 0)
{
num = int.Parse(((TextBox)this.DataList1.Items[i].FindControl("TextBox1")).Text);
}
if (Request.Cookies["CarInfo"] == null)
{
cok = new HttpCookie("CarInfo");
cok.Expires = DateTime.Now.AddDays(2);
Response.AppendCookie(cok);
}
cok = Request.Cookies["CarInfo"];
if (cok.Values.Count >= 20)
{
Response.Write("购物车已满!");
}
//这里是检查购物车里是否有这个物品,如果有,则累加
if (cok.Values[e.CommandName] == null)
{
cok.Values.Add(e.CommandName, num.ToString());
}
else
{
cok.Values[e.CommandName] = int.Parse(cok.Values[e.CommandName].ToString()) + num + "";
}
cok.Expires = DateTime.Now.AddDays(2);
Response.AppendCookie(cok);
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies["CarInfo"] == null)
{
return;
}
cok = Request.Cookies["CarInfo"];
cok.Expires = DateTime.Now.AddDays(-1);
Response.AppendCookie(cok);
}
}
(3)制作购物车页
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="shopcar.aspx.cs" Inherits="shopcar" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div><asp:Label ID="car_msg" runat="server" Text=""></asp:Label>
</div>
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate><table border="1"></HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,"select") %>'/></td>
<td><asp:Label ID="product_id" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"id") %>'></asp:Label></td>
<td><%# DataBinder.Eval(Container.DataItem,"name") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"num") %></td>
<td><asp:LinkButton ID="LinkButton1" runat="server" CommandName ='<%# DataBinder.Eval(Container.DataItem,"id") %>' oncommand="LinkButton2_Command">删除</asp:LinkButton></td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
为购物车页添加代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class shopcar : System.Web.UI.Page
{
HttpCookie cok = null;
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
database db = new database();
protected void Page_Load(object sender, EventArgs e)
{
this.car_msg.Text = "购物车为空";
if (Request.Cookies["CarInfo"] != null)
{
dt2.Columns.Add("select",typeof(bool));
dt2.Columns.Add("id");
dt2.Columns.Add("name");
dt2.Columns.Add("num");
cok = Request.Cookies["CarInfo"];
this.car_msg.Text = "共有" + Request.Cookies["CarInfo"].Values.Count + "" + "条购物记录";
//用户错按返回键时
//cok中的项不合的情况
//并且是删光购物车时才会出现这个错误
if (cok.Values[0].Length == 0)
{
cok.Expires = DateTime.Now.AddDays(-1);
Response.AppendCookie(cok);
this.car_msg.Text = "购物车为空";
this.Repeater1.DataBind();
return;
}
//因为cookie中不存放产品的名称,所以要进数据库查询一次
string SQL = "select * from product_list where ";
for (int i = 0; i < cok.Values.Count-1; i++)
{
SQL = SQL + "product_id='" + cok.Values.Keys[i] + "' or ";
}
SQL = SQL + " product_id='" + cok.Values.Keys[cok.Values.Count - 1] + "'";
dt = db.select_db(SQL);
//将查询结果加入新的dt2中,并绑定
for (int i = 0; i < cok.Values.Count; i++)
{
dt2.Rows.Add(false, cok.Values.Keys[i], dt.Rows[i][1].ToString(), cok.Values[i]);
}
//Response.Write(SQL);
this.Repeater1.DataSource = dt2;
this.Repeater1.DataBind();
}
}
protected void LinkButton2_Command(object sender, CommandEventArgs e)
{
if (Request.Cookies["CarInfo"] == null)
{
return;
}
//cookie删完之后,记得把dt2中的数据也删除
try
{
cok = Request.Cookies["CarInfo"];
cok.Values.Remove(e.CommandName);
Response.AppendCookie(cok);
dt2.Select("id='" + e.CommandName + "'")[0].Delete();
}
catch
{
//return;
}
this.Repeater1.DataSource = dt2;
this.Repeater1.DataBind();
if (dt2.Rows.Count > 0)
{
this.car_msg.Text = "共有" + Request.Cookies["CarInfo"].Values.Count + "" + "条购物记录";
}
else
{
this.car_msg.Text = "购物车为空";
}
}
}