asp.net 實現購物車詳細程式碼

iDotNetSpace發表於2008-07-03
   
shoppingcart<br> 
   
 
http://schemas.microsoft.com/intellisense/ie5"> 
 
  
                       
      
                
         
          
           
          
         
        
        
        
        
        
         
          
                    
        
        
       
      
   
                                   



      
  
 

=======================================================================================以上為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
{
    /// /// shoppingcart 的摘要說明. /// 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                {
                    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;
                    ldDR=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 );
        }
        ///   /// 設計器支援所需的方法 - 不要使用程式碼編輯器修改  /// 此方法的內容.  ///   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" );
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-369023/,如需轉載,請註明出處,否則將追究法律責任。

相關文章