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="清空购物车" onclick="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)
{
//标识产品的数量 www.zzzyk.com
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;
&
补充:Web开发 , ASP.Net ,