冷枫@CSDN

CSharp程序员大本营:积累平凡就是积累卓越!有了翅膀,你就有了天空!钝到极点的刀才最具杀伤力——因为它是锤子!

用户操作
[即时聊天] [发私信] [加为好友]
╄ 冷枫ID:JavaProgramers
483719次访问,排名98好友111人,关注者144
毕业于南阳理工学院计算机系软件工程专业,一直从事于软件开发工作,编程经验5年,一线程序员出身,开发实战经验丰富,精通Asp,Asp.NET, ERP(C#.NET),基于WEB(Asp.Net)的MIS等世道上的一般编程,精通SQL Server存储过程开发,曾为一家中型商务企业软件开发工程师,软件培训师。
JavaProgramers的文章
原创 287 篇
翻译 0 篇
转载 24 篇
评论 441 篇
╄ 冷枫的公告
╄ 冷枫 Asp.NET微软MVP
本Blog技术支持QQ群
[NET技术联盟]:1908832
[冷枫开发小组]:6307410
CSharp开放源码促进会
CSharp程序员大本营
最近评论
反对垄断:现在都在卖服务,软件都是白送的,微软降到100多就想笼络人心,做梦,微软迟早要完蛋
wyw_2002:“中国用户的计算机中60%都运行微软的操作系统“

请问这个60%是你统计的吗?
那40%是什么系统?

捡瓶子:废瓶子1角钱一个,一天捡100个,10元,跑去吃饭8元,买个XP,也要捡2年啊!
zenggang2008:反击盗版,在中国还有很长的路要走啊
大家都已经习惯"免费"了
路过:盗版不利于中国的软件业的发展,最终用户好像在盗版软件中得到了很多的好处,但是把中国的软件产业搞垮了。最终用户最后还是受害者。
文章分类
收藏
    相册
    IT英雄传奇
    大学留念集
    公司一角
    旅游系列二
    旅游系列三
    旅游系列一
    [╄ 冷枫]简介
    站长简介(RSS)
    经典网站收集
    .NET分页存储过程
    AJAX中国
    DOTNET控件网
    DotNet男孩社区
    Java共舞
    Tutorails[.NET]
    中国盟动力
    冷枫技术论坛
    开发者在线
    技术无极限(RSS)
    深圳赶集网
    源码网
    软件项目网
    网上邻居
    『 天道酬勤 』(RSS)
    『 孟子E章 』(RSS)
    『 孟子E章 』
    『 邹建专栏 』(RSS)
    『webdiyer 』
    微软官方
    ASP.NET 入门教程
    webcast视频教程
    存档
    订阅我的博客
    XML聚合  FeedSky

    原创 asp.net 实现购物车详细代码收藏

    新一篇: DataGrid使用心得 | 旧一篇: 使用ActiveX控件开发网页常见的问题

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

     

    发表于 @ 2006年12月21日 20:47:00|评论(loading...)|编辑

    新一篇: DataGrid使用心得 | 旧一篇: 使用ActiveX控件开发网页常见的问题

    评论

    #jinbo_IT 发表于2007-04-07 15:20:01  IP: 121.28.160.*

    你好!
    能把Asp的购物车的代码发给我吗?
    谢谢了
    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © ╄ 冷枫