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

asp.net 实现购物车     shoppingcart        http://schemas.microsoft.com/intellisense/ie5">                                      ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" Head 阅读详情

 
<%@ 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" );
        }
    }
}
 

 

在线商城之购物车实现——(附完整代码) 1、首页 1.1实现效果 1.2代码实现 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>在线商城</title> <script src="jquery.min.js"></script> <style> ul { list-style: none; } a { text 阅读详情

相关推荐

ASP.NET购物车的实现及结算处理源代码

 ASP.NET购物车的实现及结算处理源代码作者:Mr.zhao 本示例利用Session对象来实现一个简单的购物车。主要用于教学演示。 Book类此类主是代表购物车的一本书using System;namespace CartTest{ ///  /// Books 的摘要说明。 ///  public class Book {  string bookid;  string tit

.Net学习园地 6066

购物车的实现及结算处理

本示例利用Session对象来实现一个简单的购物车。主要用于教学演示。Book类此类主是代表购物车的一本书using System;namespace CartTest{ ///  /// Books 的摘要说明。 ///  public class Book {  string bookid;  string title;  decimal price;  int num;  pub

greystar的专栏 1万+

【ASP.NET Web】项目实践—网上宠物店9:制作“购物车”页面

在商品展示页中,每个宠物商品都有“添加到购物车”链接,用户单击该链接,该商品即放置到用户的购物车中,代表用户准备订购的商品。 分析购物车页面的界面:内容实现,要写在占位符控件中。 分析购物车页面的功能:在商品展示页中,每个商品都有一个“添加到购物车”链接,链接跳转时会传递参数:~/ShoppingCart.aspx?ProductId=产品ID&Name=产品名称&Price=单价&CategoryId=类别名称。当用户单击链接时,购物车页面需要把查询字符串中指定的商品信息添

王迪 2130

实现一个简单的购物Web应用:从登录到购物车结账

在本篇博客中,我们将讨论开发一个基本的电子商务网站,从登录到购物车结账的关键步骤。创建登录表单和Servlet:我们设计了一个简单的登录表单,用户需要输入用户名和密码。如果用户成功登录,我们将用户名存储在会话中,以便在购物车和结账过程中识别用户。订单数据通常存储在数据库中,但在本示例中,我们存储在会话中。从购物车中删除商品:我们展示了如何创建"删除"按钮,以便用户可以删除购物车中的商品。验证用户信息:在Servlet中,我们检查用户提供的用户名和密码是否与存储在数据库中的信息匹配。

WishLostmymind的博客 826

购物车完整功能代码

本代码实现了一套完整的购物车功能,批量选择、批量删除。真正告别了霸王购物车,提供完整的注释,简单易懂

asp.net 购物车的实现浅析

该购物车的功能如下: . 通过ajax实现添加和删除车上的物品。 . 删除的物品会显示出来,可以重新添加到购物车。 . 嗯…没有了,具体大家接着看吧。 购物车的结构我打算用一个table来展示,在UserControl里使用ListView展现购物车的物品(因为比拼接字符串要容易维护的多)。具体代码如下(ShopCartTest.ascx): 代码如下: <table runat=”server” cellpadding=’0′ cellspacing=’0′ widt

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

<!--google_ad_client = "pub-4490194096475053";/* 内容页,300x250,第一屏 */google_ad_slot = "3685991503";google_ad_width = 300;google_ad_height = 250;//--><script type="text/javascript"src="

管子(zero)的杂乱空间 2625

从零到一:手把手教你用Python+LangChain搭建企业级RAG知识库(附避坑指南)

本文为开发者提供了一份详尽的实战指南,手把手教你使用Python和LangChain框架从零构建企业级RAG知识库。内容涵盖环境搭建、文档处理、向量化检索、提示工程及生产部署等核心环节,并重点分享了文档分割、检索优化等关键避坑经验,旨在帮助读者打造稳定、高效且易于维护的智能知识中枢。

weixin_29058331的博客 400
上一篇: 使用ActiveX控件开发网页常见的问题
下一篇: DataGrid使用心得
JavaProgramers
博客等级 码龄22年 660粉丝 304原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址