最近在做一个商城购物的一部分,用到了购物车,因为是刚参加工作,对购物这方面,不太了解,所以用自己的方法作了一个购物车,在网上查了查资料,有的用数据库纪录购物车信息的,有的用Session纪录的,由于时间的原因,我用的是Session,如果各位有更好的建议或意见,不妨在此讨论一下,共同进步,共同学习!
一下是源代码,我在里面用的都是图片,可能用的时候图片显示不出来,在一个我用的是三层架构的,所以在用的时候把它改成您们所用的就行了
//------------------------------------------------------------------------------------------Html------------------------------------------------------------------------
<!--/**************************************************************
*
* Copyright (C)1995-2006 .
*
* Author : 兴百放
* Create Date : 2007-3-28
* Summary : 购物车管理
*
*
* Modified By :
* Date :
* Mail : xbf321@163.com Or xbf321@tom.com
* Blog : blog.csdn.net/xbf321
**************************************************************/-->
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cart.aspx.cs" Inherits="Cart" %>
<!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>
<table>
<tr>
<td colspan="4">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Pro_Id" Visible="False" />
<asp:TemplateField HeaderText="产品名称">
<ItemTemplate>
<%#Eval("Pro_Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="数量">
<ItemTemplate>
<asp:TextBox ID="txtproquantity" runat="server" Width="51px" Text='<%#Eval("Pro_Quantity") %>'></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="单价">
<ItemTemplate>
<%#Eval("Pro_Price") %>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="价格">
<ItemTemplate>
<%#Eval("Pro_SubTotal") %>
</ItemTemplate>
<FooterTemplate>
总共:<%=dTotal %>元
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td style=" 100px">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/cart_clear_zh.gif"
OnClick="ImageButton1_Click" /></td>
<td style=" 100px">
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Images/cart_change_zh.gif" OnClick="ImageButton2_Click" /></td>
<td style=" 100px">
<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/Images/cart_continue_zh.gif"
OnClick="ImageButton3_Click" /></td>
<td style=" 100px">
<asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="~/Images/cart_next_zh.gif" OnClick="ImageButton4_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
//-----------------------------------------------------------------------------------------。Cs------------------------------------------------------------------------
/**************************************************************
*
* Copyright (C)1995-2006 .
*
* Author : 兴百放
* Create Date : 2007-3-28
* Summary : 购物车管理
*
*
* Modified By :
* Date :
* Mail : xbf321@163.com Or xbf321@tom.com
* Blog : blog.csdn.net/xbf321
**************************************************************/
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;
using System.Collections.Generic;
public partial class Cart : System.Web.UI.Page
{
private string _Pro_Id;
DataTable CartTable = new DataTable(); //声明一个DataTable对象
DataRow dr;
public Double dTotal = 0.00; //总计款额
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
if (Session["CartShop"] == null) //判断是否为第一次添加购物车否则创建DataTable
{
CartTable.Columns.Add(new DataColumn("Pro_Id", typeof(string)));
CartTable.Columns.Add(new DataColumn("Pro_Name", typeof(string)));
CartTable.Columns.Add(new DataColumn("Pro_Quantity",typeof(int)));
CartTable.Columns.Add(new DataColumn("Pro_Price", typeof(decimal)));
CartTable.Columns.Add(new DataColumn("Pro_SubTotal",typeof(decimal)));
Session["CartShop"] = CartTable;
}
else
{
CartTable = (DataTable)Session["CartShop"];
}
if (Request.QueryString["proid"].ToString() != null)
{
_Pro_Id = Request.QueryString["proid"].ToString();
InsertList(_Pro_Id);
}
gvCartBind();
}
}
/// <summary>
/// 向DataTable插入数据,并判断是否为同一种商品,如果是则让数量增一,否则创建一行
/// </summary>
/// <param name="sPro_Id"></param>
public void InsertList(string sPro_Id)
{
int iPro_Quantity = 1;
bool IsSame = false; //表示是否为同一种商品
eWebShop.BLL.Product bPro = new eWebShop.BLL.Product(); //这是购物车的逻辑层
foreach (DataRow dr in CartTable.Rows) //遍历DataTable
{
if (dr[0].ToString() == sPro_Id)
{
iPro_Quantity = Convert.ToInt32(dr[2].ToString());
iPro_Quantity++;
dr[2] = iPro_Quantity;
dr[4] = iPro_Quantity * Convert.ToDecimal(dr[3].ToString());
IsSame = true;
}
}
if (!IsSame) //DataTable里没有,则创建一行该商品
{
dr = CartTable.NewRow();
dr[0] = sPro_Id;
dr[1] = bPro.GetOnlyProductById(sPro_Id).PName;
dr[2] = iPro_Quantity;
dr[3] = bPro.GetOnlyProductById(sPro_Id).PMoney;
dr[4] = iPro_Quantity * Convert.ToDecimal(dr[3].ToString());
CartTable.Rows.Add(dr);
}
Session["CartShop"] = CartTable; //添加到Session中
}
public void gvCartBind()
{
GridView1.DataSource = CartTable; //GridView绑定
GridView1.DataBind();
}
/// <summary>
/// 清空购物车
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Session["CartShop"] = null;
Response.Write("<script>window.close();</script>");
}
/// <summary>
/// 继续购买商品
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
Response.Write("<script>window.close();</script>");
}
/// <summary>
/// 得出商品总的价格
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Footer)
{
for (int i = 0; i < CartTable.Rows.Count; i++ )
{
dTotal += Convert.ToDouble(CartTable.Rows[i][4]);
}
}
}
/// <summary>
/// 调整商品数量并更新DataTable
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
CartTable = (DataTable)Session["CartShop"];
//把GirdView中的数量付给DataTable
for (int i = 0; i < GridView1.Rows.Count; i++ )
{
CartTable.Rows[i][2] = Convert.ToInt32(((TextBox)GridView1.Rows[i].FindControl("txtproquantity")).Text);
}
//然后重新遍历DataTable的总的价格
for (int i = 0; i < CartTable.Rows.Count; i++)
{
CartTable.Rows[i][4] = Convert.ToInt32(CartTable.Rows[i][2]) * Convert.ToDecimal(CartTable.Rows[i][3]);
}
//GridView重新绑定
gvCartBind();
}
/// <summary>
/// 下一步,提交
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
{
}
}
一下是源代码,我在里面用的都是图片,可能用的时候图片显示不出来,在一个我用的是三层架构的,所以在用的时候把它改成您们所用的就行了
//------------------------------------------------------------------------------------------Html------------------------------------------------------------------------
<!--/**************************************************************
*
* Copyright (C)1995-2006 .
*
* Author : 兴百放
* Create Date : 2007-3-28
* Summary : 购物车管理
*
*
* Modified By :
* Date :
* Mail : xbf321@163.com Or xbf321@tom.com
* Blog : blog.csdn.net/xbf321
**************************************************************/-->
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cart.aspx.cs" Inherits="Cart" %>
<!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>
<table>
<tr>
<td colspan="4">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Pro_Id" Visible="False" />
<asp:TemplateField HeaderText="产品名称">
<ItemTemplate>
<%#Eval("Pro_Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="数量">
<ItemTemplate>
<asp:TextBox ID="txtproquantity" runat="server" Width="51px" Text='<%#Eval("Pro_Quantity") %>'></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="单价">
<ItemTemplate>
<%#Eval("Pro_Price") %>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="价格">
<ItemTemplate>
<%#Eval("Pro_SubTotal") %>
</ItemTemplate>
<FooterTemplate>
总共:<%=dTotal %>元
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td style=" 100px">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/cart_clear_zh.gif"
OnClick="ImageButton1_Click" /></td>
<td style=" 100px">
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/Images/cart_change_zh.gif" OnClick="ImageButton2_Click" /></td>
<td style=" 100px">
<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/Images/cart_continue_zh.gif"
OnClick="ImageButton3_Click" /></td>
<td style=" 100px">
<asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="~/Images/cart_next_zh.gif" OnClick="ImageButton4_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
//-----------------------------------------------------------------------------------------。Cs------------------------------------------------------------------------
/**************************************************************
*
* Copyright (C)1995-2006 .
*
* Author : 兴百放
* Create Date : 2007-3-28
* Summary : 购物车管理
*
*
* Modified By :
* Date :
* Mail : xbf321@163.com Or xbf321@tom.com
* Blog : blog.csdn.net/xbf321
**************************************************************/
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;
using System.Collections.Generic;
public partial class Cart : System.Web.UI.Page
{
private string _Pro_Id;
DataTable CartTable = new DataTable(); //声明一个DataTable对象
DataRow dr;
public Double dTotal = 0.00; //总计款额
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
if (Session["CartShop"] == null) //判断是否为第一次添加购物车否则创建DataTable
{
CartTable.Columns.Add(new DataColumn("Pro_Id", typeof(string)));
CartTable.Columns.Add(new DataColumn("Pro_Name", typeof(string)));
CartTable.Columns.Add(new DataColumn("Pro_Quantity",typeof(int)));
CartTable.Columns.Add(new DataColumn("Pro_Price", typeof(decimal)));
CartTable.Columns.Add(new DataColumn("Pro_SubTotal",typeof(decimal)));
Session["CartShop"] = CartTable;
}
else
{
CartTable = (DataTable)Session["CartShop"];
}
if (Request.QueryString["proid"].ToString() != null)
{
_Pro_Id = Request.QueryString["proid"].ToString();
InsertList(_Pro_Id);
}
gvCartBind();
}
}
/// <summary>
/// 向DataTable插入数据,并判断是否为同一种商品,如果是则让数量增一,否则创建一行
/// </summary>
/// <param name="sPro_Id"></param>
public void InsertList(string sPro_Id)
{
int iPro_Quantity = 1;
bool IsSame = false; //表示是否为同一种商品
eWebShop.BLL.Product bPro = new eWebShop.BLL.Product(); //这是购物车的逻辑层
foreach (DataRow dr in CartTable.Rows) //遍历DataTable
{
if (dr[0].ToString() == sPro_Id)
{
iPro_Quantity = Convert.ToInt32(dr[2].ToString());
iPro_Quantity++;
dr[2] = iPro_Quantity;
dr[4] = iPro_Quantity * Convert.ToDecimal(dr[3].ToString());
IsSame = true;
}
}
if (!IsSame) //DataTable里没有,则创建一行该商品
{
dr = CartTable.NewRow();
dr[0] = sPro_Id;
dr[1] = bPro.GetOnlyProductById(sPro_Id).PName;
dr[2] = iPro_Quantity;
dr[3] = bPro.GetOnlyProductById(sPro_Id).PMoney;
dr[4] = iPro_Quantity * Convert.ToDecimal(dr[3].ToString());
CartTable.Rows.Add(dr);
}
Session["CartShop"] = CartTable; //添加到Session中
}
public void gvCartBind()
{
GridView1.DataSource = CartTable; //GridView绑定
GridView1.DataBind();
}
/// <summary>
/// 清空购物车
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Session["CartShop"] = null;
Response.Write("<script>window.close();</script>");
}
/// <summary>
/// 继续购买商品
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
Response.Write("<script>window.close();</script>");
}
/// <summary>
/// 得出商品总的价格
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Footer)
{
for (int i = 0; i < CartTable.Rows.Count; i++ )
{
dTotal += Convert.ToDouble(CartTable.Rows[i][4]);
}
}
}
/// <summary>
/// 调整商品数量并更新DataTable
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
CartTable = (DataTable)Session["CartShop"];
//把GirdView中的数量付给DataTable
for (int i = 0; i < GridView1.Rows.Count; i++ )
{
CartTable.Rows[i][2] = Convert.ToInt32(((TextBox)GridView1.Rows[i].FindControl("txtproquantity")).Text);
}
//然后重新遍历DataTable的总的价格
for (int i = 0; i < CartTable.Rows.Count; i++)
{
CartTable.Rows[i][4] = Convert.ToInt32(CartTable.Rows[i][2]) * Convert.ToDecimal(CartTable.Rows[i][3]);
}
//GridView重新绑定
gvCartBind();
}
/// <summary>
/// 下一步,提交
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
{
}
}