asp.net 购物车实现详细代码

<%@ Page language="c#" Codebehind="shoppingcart.aspx.cs" AutoEventWireup="false" Inherits="myshop.shoppingcart" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD>
<title>shoppingcart
</title>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312"> <LINK href="mycss.css" type="text/css" rel="stylesheet">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD>
<body> <center>
<form id="Form1" runat="server"> <table width="500" border="0" cellspacing="0" cellpadding="0"> <tr> <td>
<asp:DataGrid id="ShoppingCartDlt" runat="server" Width="500" BackColor="white" BorderColor="black" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#cecfd6" AutoGenerateColumns="false" MaintainState="true"> <Columns>
<asp:TemplateColumn HeaderText="删除">
<ItemTemplate> <center>
<asp:CheckBox id="chkProductID" runat="server" /> </center>
</ItemTemplate> </asp:TemplateColumn>
<asp:BoundColumn DataField="ProdID" HeaderText="ID" />
<asp:BoundColumn DataField="ProName" HeaderText="商品名称" />
<asp:BoundColumn DataField="UnitPrice" HeaderText="单价" />
<asp:TemplateColumn HeaderText="数量">
<ItemTemplate>
<asp:TextBox id="CountTb" runat="server" Text='<%#DataBinder.Eval( Container.DataItem,"ProdCount" )%>'> </asp:TextBox>
</ItemTemplate> </asp:TemplateColumn>
<asp:BoundColumn DataField="TotalPrice" HeaderText="小计( 元 )" /> </Columns> </asp:DataGrid></td> </tr> </table> <br> <table width="500" border="0" cellspacing="0" cellpadding="0"> <tr> <td>
<asp:Button id="update" runat="server" Text="更新我的购物车" CssClass="button2" /></td> <td>
<asp:Button id="CheckOut" runat="server" Text="结算" CssClass="button5" />
<input type="button" name="close2" value="继续购物" onClick="window.close( );
return false;
" class="button2"></td> <td align="right"><br>
<asp:Label id="label" runat="server" Width="100px" Visible="True" ForeColor="#FF8080" Height="18px"></asp:Label></td> </tr> </table>
</form> </center>
</body></HTML>=======================================================================================以上为HTML页面部分==========================================================================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Web.SessionState;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
namespace myshop
{
/// <summary> /// shoppingcart 的摘要说明. /// </summary> public class shoppingcart : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid ShoppingCartDlt;
protected System.Web.UI.WebControls.Button update;
protected System.Web.UI.WebControls.Button CheckOut;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.Label label;
protected System.Web.UI.WebControls.CheckBox chkProductID;
protected System.Web.UI.WebControls.TextBox txtCount;
protected System.Web.UI.WebControls.TextBox CountTb;
string AddProID;
private void Page_Load( object sender, System.EventArgs e )
{
try
{
if ( Session["logon"]!="yes"||Session["username"]==null )
{
Response.Redirect( "error.htm" ) ;
}
}
catch
{
Response.Redirect( "error.htm" ) ;
}
/////////////查看用户是否已经登陆.
if( !IsPostBack )
{
if( Request.Params["mode"]=="view" ) //检测是否为直接查看购物车.
{
ViewShoppingCart( );
Caculator( );
}
if( Request.Params["productID"]!=null||Request.Params["productID"]!="" )
{
AddProID=Request["productID"];
UpdateShoppingCart( );
Caculator( );
}
}
// 在此处放置用户代码以初始化页面
}
public void CreateCartTable( ) //创建购物车
{
DataSet ds = new DataSet( );
DataTable newDT=new DataTable( "CartTable" );
ds.Tables.Add( newDT );
DataColumn newDC;
newDC=new DataColumn( "ProdID",System.Type.GetType( "System.Int32" ) );
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "ProdCount",System.Type.GetType( "System.Int32" ) );
newDC.DefaultValue=1;
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "ProName",System.Type.GetType( "System.String" ) );
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "UnitPrice",System.Type.GetType( "System.Double" ) );
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "TotalPrice",System.Type.GetType( "System.Double" ) );
ds.Tables["CartTable"].Columns.Add( newDC );
newDC=new DataColumn( "IsDeleted",System.Type.GetType( "System.Int32" ) );
newDC.DefaultValue=0;
// public void WriteShoppingCart( ) 中 newDR[5]="0";
行,已被注销, ds.Tables["CartTable"].Columns.Add( newDC );
Session["myCartTable"]=newDT;
ShoppingCartDlt.DataSource=ds.Tables["CartTable"].DefaultView;
ShoppingCartDlt.DataBind( );
}
public void UpdateShoppingCart( )
{
if( Session["myCartTable"]==null )//Session["myCartTable"]==null
{
CreateCartTable( );
//调用函数CreateCartTable( )新建一个DataTable WriteShoppingCart( );
}
else
{
//如果购物蓝中已有商品,则需要对购物信息表DataTable进行更新,并将其棒定到ShoppingCartDlt WriteShoppingCart( );
}
}
public void ViewShoppingCart( ) //查看购物车
{
if( Session["myCartTable"]!=null )
{
DataTable viewTable=new DataTable( "nowCartTable" );
viewTable=( DataTable )Session["myCartTable"];
ShoppingCartDlt.DataSource = viewTable.DefaultView;
//购物车棒定到ShoppingCartDlt ShoppingCartDlt.DataBind( );
}
}
public void WriteShoppingCart( )
{
if( Request.Params["mode"]!="view" ) //检查是否是直接查看购物车,如果直接查看,就不再写MYCARTTABLE
{
DataTable nowTable=new DataTable( "nowCartTable" );
nowTable=( DataTable )Session["myCartTable"];
int pn=nowTable.Rows.Count;
int i=0;
bool hasone=false;
int nowProdID;
while( i<pn && !hasone )
{
nowProdID=Int32.Parse( nowTable.Rows[i][0].ToString( ) );
if( nowProdID==Int32.Parse( AddProID ) ) //判断购物信息表中,是否存有当前放入商品. if( nowProdID==Int32.Parse( AddProID ) )
{
hasone=true;
}
else
{
i++;
}
}
if( hasone )
{
//如果已有该商品,则 hasone=true,更改该数据行 DataRow oldDR;
oldDR=nowTable.Rows[i];
oldDR["ProdCount"]=Int32.Parse( oldDR["ProdCount"].ToString( ) )+1;
oldDR["TotalPrice"]=Int32.Parse( oldDR["ProdCount"].ToString( ) )*Double.Parse( oldDR["UnitPrice"].ToString( ) );
}
else
{
//如果没有该商品,在表中新加如一行. DataRow newDR;
double unitp;
String strcon="provider=Microsoft.jet.OLEDB.4.0;
data Source="+Server.MapPath( ConfigurationSettings.AppSettings["MDBpath2"] )+";
";
OleDbConnection myConnection = new OleDbConnection( strcon );
string strSQL= "select * from pro where product_id="+AddProID+"";
OleDbDataAdapter myCommand = new OleDbDataAdapter( strSQL, myConnection );
DataSet ds = new DataSet( );
myCommand.Fill( ds, "AddP" );
newDR=nowTable.NewRow( );
newDR[0]=AddProID;
newDR[2]=ds.Tables["Addp"].Rows[0]["product_name"].ToString( );
unitp=Double.Parse( ds.Tables["AddP"].Rows[0]["product_memprice"].ToString( ) );
//会员价 newDR[3]=unitp;
newDR[4]=unitp;
//第一次读库,所以总价格和单价是一样的. //newDR[5]="0";
nowTable.Rows.Add( newDR );
myConnection.Close( );
}
ShoppingCartDlt.DataSource = nowTable.DefaultView;
//将更新后的 DataTable棒定到ShoppingCartDlt ShoppingCartDlt.DataBind( );
Session["myCartTable"] = nowTable;
//重新保存更新过的DataTable
}
}
public void Caculator( )
{
if( Session["myCartTable"]!=null ) //购物车是否为空
{
int h;
Double TotalPri;
TotalPri=0;
DataTable nowTable3=new DataTable( "nowCartTable3" );
nowTable3=( DataTable )Session["myCartTable"];
if( nowTable3.Rows.Count>0 ) //返回购物车中是否有货物
{
for( h=0;
h<=nowTable3.Rows.Count-1;
h++ )
{
TotalPri=TotalPri+Int32.Parse( nowTable3.Rows[h][4].ToString( ) );
//Double.Parse( ( string )TotalText.Text );
}
label.Text="总计: "+TotalPri.ToString( )+" 元" ;
}
}
}
public void Update( )
{
int i;
int j;
int k;
ArrayList deleteItem = new ArrayList( 10 );
DataGridItem _item ;
j=0;
int deleteid;
k=0;
DataTable nowTable2=new DataTable( "nowCartTable2" );
nowTable2=( DataTable )Session["myCartTable"];
for( i=0;
i<=this.ShoppingCartDlt.Items.Count-1;
i++ )
{
_item = this.ShoppingCartDlt.Items[i];
TextBox CountText=( TextBox )this.ShoppingCartDlt.Items[i].Cells[4].FindControl( "CountTb" );
//Controls[1];
//_item.FindControl( "CountTb" );
CheckBox ProductIDCheck =( CheckBox ) _item.FindControl( "chkProductID" );
nowTable2.Rows[i][1] = Int32.Parse( CountText.Text.ToString( ) );
nowTable2.Rows[i][4] = Int32.Parse( nowTable2.Rows[i][1].ToString( ) ) * Double.Parse( nowTable2.Rows[i][3].ToString( ) );
if( ProductIDCheck.Checked )
{
nowTable2.Rows[i][5] = 1;
//添加删除标记1 j=j+1;
}
}
string strExpr="IsDeleted>0";
//http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfSystemDataDataTableClassSelectTopic.asp DataRow[] foundRows = nowTable2.Select( strExpr );
for( int m = 0;
m < foundRows.Length;
m ++ )
{
//Console.WriteLine( foundRows[i][0] );
foundRows[m].Delete( );
}
ShoppingCartDlt.DataSource = nowTable2.DefaultView;
ShoppingCartDlt.DataBind( );
Session["myCartTable"] = nowTable2;
Caculator( );
}
#region Web 窗体设计器生成的代码 override protected void OnInit( EventArgs e )
{
// // CODEGEN: 该调用是 asp.NET Web 窗体设计器所必需的. // InitializeComponent( );
base.OnInit( e );
}
/// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容. /// </summary> private void InitializeComponent( )
{
this.update.Click += new System.EventHandler( this.update_Click );
this.CheckOut.Click += new System.EventHandler( this.CheckOut_Click );
this.Load += new System.EventHandler( this.Page_Load );
}
#endregion
private void update_Click( object sender, System.EventArgs e )
{
Update( );
}
private void CheckOut_Click( object sender, System.EventArgs e )
{
Update( );
Response.Redirect( "checkout.aspx" );
}
}
}

(0)

相关推荐

  • php 购物车完整实现代码

    1.商品展示页面 复制代码 代码如下: <table width="255"  border="0" cellspacing="0" cellpadding="0"><tr><td width="130" rowspan="6"><div align="center"><?php        if(trim($i

  • Android实现的仿淘宝购物车demo示例

    本文实例讲述了Android实现的仿淘宝购物车.分享给大家供大家参考,具体如下: 夏的热情渐渐退去,秋如期而至,丰收的季节,小编继续着实习之路,走着走着,就走到了购物车,逛过淘宝或者是京东的小伙伴都知道购物车里面的宝贝可不止一件,对于爱购物的姑娘来说,购物车里面的商品恐怕是爆满,添加不进去了,以前逛淘宝的时候,小编没有想过要怎么样实现购物车,就知道在哪儿一个劲儿的逛,但是现在不一样了,小编做为一个开发者,想的就是该如何实现,捣鼓了两天的时间,用listview来实现,已经有模有样了,现在小编就来

  • Android实现购物车功能

    最近看了一些淘宝购物车的demo,于是也写了一个. 效果图如下: 主要代码如下: actvity中的代码: public class ShoppingCartActivity extends BaseActivity { private List<Test> data; private ListView mListView; private ShoppingCartAdapter adapter; private RelativeLayout rlRefresh; private TextVi

  • java web开发之实现购物车功能

    为了方便自己以后复习,所以写的比较仔细,记录下自己的成长.  既然是做购物车,那么前提条件是首先需要一系列商品,也就是要建一个实体,这里建了一个商品表. 通过查询在浏览器上显示 基本显示已经做好了,现在进入我们的重头戏,Servlet  点击放入购物车时,将访问Servlet 购物车代码 package com.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; im

  • Android制作简单的普通购物车

    本文实例为大家分享了Android普通购物车制作过程,供大家参考,具体内容如下 1.最新项目新增了类似购物车功能,如下图所示: 当时刚看到此页面的时候,第一反应是利用 ListView嵌套Listview,经过一番操作最终也实现了此功能.当时也没有考虑性能问题,只考虑能写出来.后来嵌套数据,当数据量较大时,滑动Listview可以明显感觉到卡顿,这对用户来说是很难忍受的,所以才有了找到替代方案的想法,看到网上主流的是用ExpandableListView来实现此功能,所以我也用此方案来写一下.

  • php购物车实现代码

    ShopCar.php 复制代码 代码如下: <?php class Shopcar { //商品列表 public $productList=array(); /** * * @param unknown_type $product 传进来的商品 * @return true 购物车里面没有该商品 */ public function checkProduct($product) { for($i=0;$i<count($this->productList);$i++ ) { if($

  • php 购物车的例子

    //购物车session的产生代码 if(! $session && ! $scid) { /* session用来区别每一个购物车,相当于每个车的身份证号: scid只用来标识一个购物车id号,可以看做是每个车的名字: 当该购物车的id和session值两者都不存在时,就产生一个新购物车 */ $session = md5(uniqid(rand())); /* 产生一个唯一的购物车session号 rand()先产生个随机数,uniqid()再在该随机数的基础上产生一个独一无二的字符串

  • Android仿外卖购物车功能

    先看看效果图: 知识点分析 效果图来看不复杂内容并没多少,值得介绍一下的知识点也就下面几个吧 - 列表标题悬停 - 左右列表滑动时联动 - 添加商品时的抛物线动画 - 底部弹出购物车清单 - 数据的同步 另外就是实现效果的时候可能会遇到的几个坑... 布局很简单直接进入代码 1:列表标题悬停 现在做项目列表什么的基本抛弃了ListView改用RecyclerView,上篇博客中的标题悬停也是使用了一个RecyclerView的开源项目sticky-headers-recyclerview,不过写

  • Android实现仿淘宝购物车增加和减少商品数量功能demo示例

    本文实例讲述了Android实现仿淘宝购物车增加和减少商品数量功能.分享给大家供大家参考,具体如下: 在前面一篇<Android实现的仿淘宝购物车demo示例>中,小编简单的介绍了如何使用listview来实现购物车,但是仅仅是简单的实现了列表的功能,随之而来一个新的问题,买商品的时候,我们可能不止想买一件商品,想买多个,或许有因为某种原因点错了,本来想买一件来着,小手不小心抖了一下,把数量错点成了三个,这个时候就涉及到一个新的功能,那就是增加和减少商品的数量,今天这篇博文,小编就来和小伙伴们

  • jQuery实现购物车多物品数量的加减+总价计算

    复制代码 代码如下: <!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> <title>jQuery实现购物

随机推荐