实现购物车详细代码vs03

 1 ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" CodeFile="Default2.aspx.cs" AutoEventWireup="false" Inherits="myshop.shoppingcart"  %> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  > < HTML >   < HEAD >  
 2 None.gif < title > shoppingcart </ title >  
 3 None.gif < meta  http-equiv ="Content-Type"  content ="text/html; 
 4 None.gifcharset=gb2312" >   < LINK  href ="mycss.css"  type ="text/css"  rel ="stylesheet" >  
 5 None.gif < meta  name ="vs_defaultClientScript"  content ="JavaScript" >  
 6 None.gif < meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >   </ HEAD >  
 7 None.gif < body >   < center >  
 8 None.gif < form  id ="Form1"  runat ="server" >   < table  width ="500"  border ="0"  cellspacing ="0"  cellpadding ="0" >   < tr >   < td >  
 9 None.gif < 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 >  
10 None.gif < asp:TemplateColumn  HeaderText ="删除" >  
11 None.gif < ItemTemplate >   < center >  
12 None.gif < asp:CheckBox  id ="chkProductID"  runat ="server"   />   </ center >  
13 None.gif </ ItemTemplate >   </ asp:TemplateColumn >  
14 None.gif < asp:BoundColumn  DataField ="ProdID"  HeaderText ="ID"   />  
15 None.gif < asp:BoundColumn  DataField ="ProName"  HeaderText ="商品名称"   />  
16 None.gif < asp:BoundColumn  DataField ="UnitPrice"  HeaderText ="单价"   />  
17 None.gif < asp:TemplateColumn  HeaderText ="数量" >  
18 None.gif < ItemTemplate >  
19 None.gif < asp:TextBox  id ="CountTb"  runat ="server"  Text ='<%#DataBinder.Eval(  Container.DataItem,"ProdCount" )% > '>  </ asp:TextBox >  
20 None.gif </ ItemTemplate >   </ asp:TemplateColumn >  
21 None.gif < asp:BoundColumn  DataField ="TotalPrice"  HeaderText ="小计( 元 )"   />   </ Columns >   </ asp:DataGrid ></ td >   </ tr >   </ table >   < br >   < table  width ="500"  border ="0"  cellspacing ="0"  cellpadding ="0" >   < tr >   < td >  
22 None.gif < asp:Button  id ="update"  runat ="server"  Text ="更新我的购物车"  CssClass ="button2"  OnClick ="update_Click"   /></ td >   < td >  
23 None.gif < asp:Button  id ="CheckOut"  runat ="server"  Text ="结算"  CssClass ="button5"   />  
24 None.gif
25 None.gif < input  type ="button"  name ="close2"  value ="继续购物"  onClick ="window.close( ); 
26 None.gifreturn false; 
27 None.gif"  class ="button2" ></ td >   < td  align ="right" >< br >  
28 None.gif < asp:Label  id ="label"  runat ="server"  Width ="100px"  Visible ="True"  ForeColor ="#FF8080"  Height ="18px" ></ asp:Label ></ td >   </ tr >   </ table >  
29 None.gif </ form >   </ center >  
30 None.gif </ body ></ HTML >
  1 None.gif using  System; 
  2 None.gif using  System.Collections; 
  3 None.gif using  System.ComponentModel; 
  4 None.gif using  System.Web.SessionState;
  5 None.gif using  System.Web; 
  6 None.gif using  System.Web.UI; 
  7 None.gif using  System.Web.UI.HtmlControls; 
  8 None.gif using  System.Web.UI.WebControls; 
  9 None.gif using  System.Data; 
 10 None.gif using  System.Data.OleDb; 
 11 None.gif using  System.Configuration;
 12 None.gif namespace  myshop
 13 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary> /// shoppingcart 的摘要说明. /// </summary> 
 15ExpandedSubBlockEnd.gif    public class shoppingcart : System.Web.UI.Page

 16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 17InBlock.gif        string AddProID;
 18InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 20InBlock.gif            try
 21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 22InBlock.gif                if (Session["logon"!= "yes" || Session["username"== null)
 23ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 24InBlock.gif                    Response.Redirect("error.htm");
 25ExpandedSubBlockEnd.gif                }

 26ExpandedSubBlockEnd.gif            }

 27InBlock.gif            catch
 28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 29InBlock.gif                Response.Redirect("error.htm");
 30ExpandedSubBlockEnd.gif            }

 31ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//查看用户是否已经登陆. 
 32InBlock.gif            if (!IsPostBack)
 33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 34InBlock.gif                if (Request.Params["mode"== "view"//检测是否为直接查看购物车. 
 35ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 36InBlock.gif                    ViewShoppingCart();
 37InBlock.gif                    Caculator();
 38ExpandedSubBlockEnd.gif                }

 39InBlock.gif                if (Request.Params["productID"!= null || Request.Params["productID"!= "")
 40ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 41InBlock.gif                    AddProID = Request["productID"];
 42InBlock.gif                    UpdateShoppingCart();
 43InBlock.gif                    Caculator();
 44ExpandedSubBlockEnd.gif                }

 45ExpandedSubBlockEnd.gif            }

 46InBlock.gif            // 在此处放置用户代码以初始化页面 
 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif        public void CreateCartTable() //创建购物车 
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 50InBlock.gif            DataSet ds = new DataSet();
 51InBlock.gif            DataTable newDT = new DataTable("CartTable");
 52InBlock.gif            ds.Tables.Add(newDT);
 53InBlock.gif            DataColumn newDC;
 54InBlock.gif            newDC = new DataColumn("ProdID", System.Type.GetType("System.Int32"));
 55InBlock.gif            ds.Tables["CartTable"].Columns.Add(newDC);
 56InBlock.gif            newDC = new DataColumn("ProdCount", System.Type.GetType("System.Int32"));
 57InBlock.gif            newDC.DefaultValue = 1;
 58InBlock.gif            ds.Tables["CartTable"].Columns.Add(newDC);
 59InBlock.gif            newDC = new DataColumn("ProName", System.Type.GetType("System.String"));
 60InBlock.gif            ds.Tables["CartTable"].Columns.Add(newDC);
 61InBlock.gif            newDC = new DataColumn("UnitPrice", System.Type.GetType("System.Double"));
 62InBlock.gif            ds.Tables["CartTable"].Columns.Add(newDC);
 63InBlock.gif            newDC = new DataColumn("TotalPrice", System.Type.GetType("System.Double"));
 64InBlock.gif            ds.Tables["CartTable"].Columns.Add(newDC);
 65InBlock.gif            newDC = new DataColumn("IsDeleted", System.Type.GetType("System.Int32"));
 66InBlock.gif            newDC.DefaultValue = 0;
 67InBlock.gif            // public void WriteShoppingCart( ) 中 newDR[5]="0";行,已被注销, ds.Tables["CartTable"].Columns.Add( newDC ); 
 68InBlock.gif            Session["myCartTable"= newDT;
 69InBlock.gif            ShoppingCartDlt.DataSource = ds.Tables["CartTable"].DefaultView;
 70InBlock.gif            ShoppingCartDlt.DataBind();
 71ExpandedSubBlockEnd.gif        }

 72InBlock.gif
 73InBlock.gif        public void UpdateShoppingCart()
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75InBlock.gif            if (Session["myCartTable"== null)//Session["myCartTable"]==null 
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 77InBlock.gif                CreateCartTable();
 78InBlock.gif                //调用函数CreateCartTable( )新建一个DataTable WriteShoppingCart( ); 
 79ExpandedSubBlockEnd.gif            }

 80InBlock.gif            else
 81ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 82InBlock.gif                //如果购物蓝中已有商品,则需要对购物信息表DataTable进行更新,并将其棒定到ShoppingCartDlt WriteShoppingCart( ); 
 83ExpandedSubBlockEnd.gif            }

 84ExpandedSubBlockEnd.gif        }

 85InBlock.gif        public void ViewShoppingCart() //查看购物车 
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 87InBlock.gif            if (Session["myCartTable"!= null)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 89InBlock.gif                DataTable viewTable = new DataTable("nowCartTable");
 90InBlock.gif                viewTable = (DataTable)Session["myCartTable"];
 91InBlock.gif                ShoppingCartDlt.DataSource = viewTable.DefaultView;
 92InBlock.gif                //购物车棒定到ShoppingCartDlt ShoppingCartDlt.DataBind( ); 
 93ExpandedSubBlockEnd.gif            }

 94ExpandedSubBlockEnd.gif        }

 95InBlock.gif        public void WriteShoppingCart()
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 97InBlock.gif            if (Request.Params["mode"!= "view"//检查是否是直接查看购物车,如果直接查看,就不再写MYCARTTABLE 
 98ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 99InBlock.gif                DataTable nowTable = new DataTable("nowCartTable");
100InBlock.gif                nowTable = (DataTable)Session["myCartTable"];
101InBlock.gif                int pn = nowTable.Rows.Count;
102InBlock.gif                int i = 0;
103InBlock.gif                bool hasone = false;
104InBlock.gif                int nowProdID;
105InBlock.gif                while (i < pn && !hasone)
106ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
107InBlock.gif                    nowProdID = Int32.Parse(nowTable.Rows[i][0].ToString());
108InBlock.gif                    if (nowProdID == Int32.Parse(AddProID)) //判断购物信息表中,是否存有当前放入商品. if( nowProdID==Int32.Parse( AddProID ) ) 
109ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
110InBlock.gif                        hasone = true;
111ExpandedSubBlockEnd.gif                    }

112InBlock.gif                    else
113ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
114InBlock.gif                        i++;
115ExpandedSubBlockEnd.gif                    }

116ExpandedSubBlockEnd.gif                }

117InBlock.gif                if (hasone)
118ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
119InBlock.gif                    //如果已有该商品,则 hasone=true,更改该数据行 DataRow oldDR; 
120InBlock.gif                    oldDR = nowTable.Rows[i];
121InBlock.gif                    oldDR["ProdCount"= Int32.Parse(oldDR["ProdCount"].ToString()) + 1;
122InBlock.gif                    oldDR["TotalPrice"= Int32.Parse(oldDR["ProdCount"].ToString()) * Double.Parse(oldDR["UnitPrice"].ToString());
123ExpandedSubBlockEnd.gif                }

124InBlock.gif                else
125ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
126InBlock.gif                    //如果没有该商品,在表中新加如一行. DataRow newDR; 
127InBlock.gif                    double unitp;
128InBlock.gif                    String strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source=" + Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"]) + ";";
129InBlock.gif                    OleDbConnection myConnection = new OleDbConnection(strcon);
130InBlock.gif                    string strSQL = "select * from pro where product_id=" + AddProID + "";
131InBlock.gif                    OleDbDataAdapter myCommand = new OleDbDataAdapter(strSQL, myConnection);
132InBlock.gif                    DataSet ds = new DataSet();
133InBlock.gif                    myCommand.Fill(ds, "AddP");
134InBlock.gif                    newDR = nowTable.NewRow();
135InBlock.gif                    newDR[0= AddProID;
136InBlock.gif                    newDR[2= ds.Tables["Addp"].Rows[0]["product_name"].ToString();
137InBlock.gif                    unitp = Double.Parse(ds.Tables["AddP"].Rows[0]["product_memprice"].ToString());
138InBlock.gif                    //会员价 newDR[3]=unitp; 
139InBlock.gif                    newDR[4= unitp;
140InBlock.gif                    //第一次读库,所以总价格和单价是一样的. //newDR[5]="0"; 
141InBlock.gif                    nowTable.Rows.Add(newDR);
142InBlock.gif                    myConnection.Close();
143ExpandedSubBlockEnd.gif                }

144InBlock.gif                ShoppingCartDlt.DataSource = nowTable.DefaultView;
145InBlock.gif                //将更新后的 DataTable棒定到ShoppingCartDlt ShoppingCartDlt.DataBind( ); 
146InBlock.gif                Session["myCartTable"= nowTable;
147InBlock.gif                //重新保存更新过的DataTable 
148ExpandedSubBlockEnd.gif            }

149ExpandedSubBlockEnd.gif        }

150InBlock.gif        public void Caculator()
151ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
152InBlock.gif            if (Session["myCartTable"!= null//购物车是否为空 
153ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
154InBlock.gif                int h;
155InBlock.gif                Double TotalPri;
156InBlock.gif                TotalPri = 0;
157InBlock.gif                DataTable nowTable3 = new DataTable("nowCartTable3");
158InBlock.gif                nowTable3 = (DataTable)Session["myCartTable"];
159InBlock.gif                if (nowTable3.Rows.Count > 0//返回购物车中是否有货物 
160ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
161InBlock.gif                    for (h = 0;
162InBlock.gif                    h <= nowTable3.Rows.Count - 1;
163InBlock.gif                    h++)
164ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
165InBlock.gif                        TotalPri = TotalPri + Int32.Parse(nowTable3.Rows[h][4].ToString());
166InBlock.gif                        //Double.Parse( ( string )TotalText.Text ); 
167ExpandedSubBlockEnd.gif                    }

168InBlock.gif                    label.Text = "总计: " + TotalPri.ToString() + " 元";
169ExpandedSubBlockEnd.gif                }

170ExpandedSubBlockEnd.gif            }

171ExpandedSubBlockEnd.gif        }

172InBlock.gif
173InBlock.gif
174InBlock.gif        public void Update()
175ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
176InBlock.gif            int i;
177InBlock.gif            int j;
178InBlock.gif            int k;
179InBlock.gif            ArrayList deleteItem = new ArrayList(10);
180InBlock.gif            DataGridItem _item;
181InBlock.gif            j = 0;
182InBlock.gif            int deleteid;
183InBlock.gif            k = 0;
184InBlock.gif            DataTable nowTable2 = new DataTable("nowCartTable2");
185InBlock.gif            nowTable2 = (DataTable)Session["myCartTable"];
186InBlock.gif            for (i = 0;
187InBlock.gif            i <= this.ShoppingCartDlt.Items.Count - 1;
188InBlock.gif            i++)
189ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
190InBlock.gif                _item = this.ShoppingCartDlt.Items[i];
191InBlock.gif                TextBox CountText = (TextBox)this.ShoppingCartDlt.Items[i].Cells[4].FindControl("CountTb");
192InBlock.gif                //Controls[1]; 
193InBlock.gif                //_item.FindControl( "CountTb" ); 
194InBlock.gif                CheckBox ProductIDCheck = (CheckBox)_item.FindControl("chkProductID");
195InBlock.gif                nowTable2.Rows[i][1= Int32.Parse(CountText.Text.ToString());
196InBlock.gif                nowTable2.Rows[i][4= Int32.Parse(nowTable2.Rows[i][1].ToString()) * Double.Parse(nowTable2.Rows[i][3].ToString());
197InBlock.gif                if (ProductIDCheck.Checked)
198ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
199InBlock.gif                    nowTable2.Rows[i][5= 1;
200InBlock.gif                    //添加删除标记1 j=j+1; 
201ExpandedSubBlockEnd.gif                }

202ExpandedSubBlockEnd.gif            }

203InBlock.gif            string strExpr = "IsDeleted>0";
204InBlock.gif            //http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfSystemDataDataTableClassSelectTopic.asp DataRow[] foundRows = nowTable2.Select( strExpr ); 
205InBlock.gif            for (int m = 0;
206InBlock.gif            m < foundRows.Length;
207InBlock.gif            m++)
208ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
209InBlock.gif                //Console.WriteLine( foundRows[i][0] ); 
210InBlock.gif                foundRows[m].Delete();
211ExpandedSubBlockEnd.gif            }

212InBlock.gif            ShoppingCartDlt.DataSource = nowTable2.DefaultView;
213InBlock.gif            ShoppingCartDlt.DataBind();
214InBlock.gif            Session["myCartTable"= nowTable2;
215InBlock.gif            Caculator();
216ExpandedSubBlockEnd.gif        }

217ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
218InBlock.gif        override protected void OnInit(EventArgs e)
219ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
220InBlock.gif            // // CODEGEN: 该调用是 asp.NET Web 窗体设计器所必需的. // InitializeComponent( ); 
221InBlock.gif            base.OnInit(e);
222ExpandedSubBlockEnd.gif        }

223ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容. /// </summary> 
224ExpandedSubBlockEnd.gif        private void InitializeComponent()

225ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
226InBlock.gif            this.update.Click += new System.EventHandler(this.update_Click);
227InBlock.gif            this.CheckOut.Click += new System.EventHandler(this.CheckOut_Click);
228InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
229ExpandedSubBlockEnd.gif        }

230ExpandedSubBlockEnd.gif        #endregion

231InBlock.gif        private void update_Click(object sender, System.EventArgs e)
232ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
233InBlock.gif            Update();
234ExpandedSubBlockEnd.gif        }

235InBlock.gif        private void CheckOut_Click(object sender, System.EventArgs e)
236ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
237InBlock.gif            Update();
238InBlock.gif            Response.Redirect("checkout.aspx");
239ExpandedSubBlockEnd.gif        }

240ExpandedSubBlockEnd.gif    }

241ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/zwl12549/archive/2006/12/31/608915.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值