购物车功能的现实vb版

网上找的购物车都是用C#写的.在此我参考了其他的代码转换成用VB写的.

shoppingcart.aspx
<% @ Page Language="vb" AutoEventWireup="false" Codebehind="ShoppingCart.aspx.vb" Inherits="MyShopping.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  content ="JavaScript"  name ="vs_defaultClientScript" >
        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
    
</ HEAD >
    
< body >
        
< center >
            
< form  id ="Form1"  runat ="server" >
                
< table  cellSpacing ="0"  cellPadding ="0"  width ="500"  border ="0" >
                    
< tr >
                        
< td >< ASP:DATAGRID  id ="ShoppingCartDlt"  runat ="server"  MaintainState ="true"  AutoGenerateColumns ="False"
                                HeaderStyle-BackColor
="#cecfd6"  Font-Size ="8pt"  Font-Name ="Verdana"  CellPadding ="3"  BorderColor ="Black"
                                BackColor
="White"  Width ="500px"  Font-Names ="Verdana" >
                                
< HeaderStyle  BackColor ="#CECFD6" ></ HeaderStyle >
                                
< Columns >
                                    
< asp:TemplateColumn  HeaderText ="删除" >
                                        
< ItemTemplate >
                                            
< asp:CheckBox  id ="chkProductID"  runat ="server" ></ asp:CheckBox >
                                        
</ ItemTemplate >
                                    
</ asp:TemplateColumn >
                                    
< asp:BoundColumn  Visible ="False"  DataField ="ProdID"  HeaderText ="ID" ></ asp:BoundColumn >
                                    
< asp:BoundColumn  DataField ="ProName"  HeaderText ="商品名称" ></ asp:BoundColumn >
                                    
< asp:BoundColumn  DataField ="UnitPrice"  HeaderText ="单价" ></ asp:BoundColumn >
                                    
< 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 ="小计(元)" ></ asp:BoundColumn >
                                
</ Columns >
                            
</ ASP:DATAGRID ></ td >
                    
</ tr >
                
</ table >
                
< br >
                
< table  cellSpacing ="0"  cellPadding ="0"  width ="500"  border ="0" >
                    
< tr >
                        
< td >< asp:button  id ="update"  runat ="server"  Width ="104px"  CssClass ="button2"  Text ="更新我的购物车" ></ asp:button > &nbsp;
                            
< asp:button  id ="bt_clean"  runat ="server"  Text ="清空购物车" ></ asp:button ></ td >
                        
< td >< asp:button  id ="CheckOut"  runat ="server"  CssClass ="button5"  Text ="结算" ></ asp:button >< input  class ="button2"  onclick ="window.close();return false;"  type ="button"  value ="继续购物"
                                name
="close2" ></ td >
                        
< td  align ="right" >< br >
                            
< asp:label  id ="label"  runat ="server"  Width ="100px"  Height ="18px"  ForeColor ="#FF8080"  Visible ="True" ></ asp:label ></ td >
                    
</ tr >
                
</ table >
            
</ form >
        
</ center >
    
</ body >
</ HTML >

后台代码
ShoppingCart.aspx.vb
Imports  System.Data.OleDb
Public   Class ShoppingCart
    
Inherits System.Web.UI.Page

Web 窗体设计器生成的代码

    
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
'在此处放置初始化页的用户代码
        Try
            
If Session("member"= "" Or Session("member"Is Nothing Then
                ShowMsg(
"未登陆""login.aspx")
            
End If
        
Catch
        
End Try
        
'///////////查看用户是否已经登陆。
        If Not IsPostBack Then
            
If Request.Params("mode"= "view" Then '检测是否为直接查看购物车。
                ViewShoppingCart()
                Caculator()
            
End If
            
If Not (Request.Params("productID"Is NothingOr Request.Params("productID"<> "" Then
                AddProID 
= Request("productID")
                UpdateShoppingCart()
                Caculator()
            
End If
        
End If ' 在此处放置用户代码以初始化页面
    End Sub




    
Public Sub CreateCartTable() '创建购物车
        Dim ds As New DataSet
        
Dim newDT As New DataTable("CartTable")
        ds.Tables.Add(newDT)
        
Dim newDC As DataColumn
        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()
    
End Sub
 'CreateCartTable


    
Public Sub UpdateShoppingCart()
        
If Session("myCartTable"Is Nothing Then 'Session["myCartTable"]==null
            CreateCartTable() '调用函数CreateCartTable()新建一个DataTable
            WriteShoppingCart()


        
Else '如果购物蓝中已有商品,则需要对购物信息表DataTable进行更新,并将其棒定到ShoppingCartDlt
            WriteShoppingCart()
        
End If
    
End Sub
 'UpdateShoppingCart


    
Public Sub ViewShoppingCart() '查看购物车
        If Not (Session("myCartTable"Is NothingThen
            
Dim viewTable As New DataTable("nowCartTable")
            viewTable 
= CType(Session("myCartTable"), DataTable)
            ShoppingCartDlt.DataSource 
= viewTable.DefaultView '购物车棒定到ShoppingCartDlt
            ShoppingCartDlt.DataBind()
        
End If
    
End Sub
 'ViewShoppingCart


    
Public Sub WriteShoppingCart()
        
If Request.Params("mode"<> "view" Then '检查是否是直接查看购物车,如果直接查看,就不再写MYCARTTABLE
            Dim nowTable As New DataTable("nowCartTable")
            nowTable 
= CType(Session("myCartTable"), DataTable)
            
Dim pn As Integer = nowTable.Rows.Count

            
Dim i As Integer = 0
            
Dim hasone As Boolean = False
            
Dim nowProdID As Integer

            
While i < pn And Not hasone
                nowProdID 
= Int32.Parse(nowTable.Rows(i)(0).ToString())
                
If nowProdID = Int32.Parse(AddProID) Then '判断购物信息表中,是否存有当前放入商品。 if(nowProdID==Int32.Parse(AddProID))
                    hasone = True
                
Else
                    i 
+= 1
                
End If
            
End While
            
If hasone Then
                
'如果已有该商品,则 hasone=true,更改该数据行
                Dim oldDR As DataRow
                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 '如果没有该商品,在表中新加如一行。
                Dim newDR As DataRow
                
Dim unitp As Double
                
Dim strcon As String = ConfigurationSettings.AppSettings("dbconn").ToString
                
Dim myConnection As New OleDbConnection(strcon)
                
Dim strSQL As String = "select *  from t_res_product where id=" + AddProID + ""
                
Dim myCommand As New OleDbDataAdapter(strSQL, myConnection)
                
Dim ds As New DataSet
                myCommand.Fill(ds, 
"AddP")

                newDR 
= nowTable.NewRow()
                newDR(
0= AddProID

                newDR(
2= ds.Tables("Addp").Rows(0)("Name").ToString()
                unitp 
= [Double].Parse(ds.Tables("AddP").Rows(0)("MemberPrice").ToString()) '会员价
                newDR(3= unitp
                newDR(
4= unitp '第一次读库,所以总价格和单价是一样的。
                'newDR[5]="0";
                nowTable.Rows.Add(newDR)

                myConnection.Close()
            
End If

            ShoppingCartDlt.DataSource 
= nowTable.DefaultView '将更新后的 DataTable棒定到ShoppingCartDlt
            ShoppingCartDlt.DataBind()

            Session(
"myCartTable"= nowTable
        
End If '重新保存更新过的DataTable
    End Sub
 'WriteShoppingCart




    
Public Sub Caculator()
        
If Not (Session("myCartTable"Is NothingThen '购物车是否为空
            Dim h As Integer
            
Dim TotalPri As [Double]
            TotalPri 
= 0
            
Dim nowTable3 As New DataTable("nowCartTable3")
            nowTable3 
= CType(Session("myCartTable"), DataTable)
            
If nowTable3.Rows.Count > 0 Then '返回购物车中是否有货物
                For h = 0 To nowTable3.Rows.Count - 1
                    TotalPri 
= TotalPri + Int32.Parse(nowTable3.Rows(h)(4).ToString()) 'Double.Parse((string)TotalText.Text);
                Next h
                label.Text 
= "总计: " + TotalPri.ToString() + " 元"
            
End If
        
End If
    
End Sub
 'Caculator


    
Public Sub ShoppingUpdate()

        
Dim i As Integer
        
Dim j As Integer
        
Dim k As Integer
        
Dim deleteItem As New ArrayList(10)
        
Dim _item As DataGridItem
        j 
= 0
        
Dim deleteid As Integer


        k 
= 0
        
Dim nowTable2 As New DataTable("nowCartTable2")
        nowTable2 
= CType(Session("myCartTable"), DataTable)


        
For i = 0 To Me.ShoppingCartDlt.Items.Count - 1
            _item 
= Me.ShoppingCartDlt.Items(i)
            
Dim CountText As TextBox = CType(Me.ShoppingCartDlt.Items(i).Cells(4).FindControl("CountTb"), TextBox) 'Controls[1];//_item.FindControl("CountTb");
            Dim ProductIDCheck As CheckBox = CType(_item.FindControl("chkProductID"), CheckBox)

            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 Then
                nowTable2.Rows(i)(
5= 1 '添加删除标记1
                j = j + 1
            
End If
        
Next i



        
Dim strExpr As String = "IsDeleted>0" 'http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfSystemDataDataTableClassSelectTopic.asp
        Dim foundRows As DataRow() = nowTable2.Select(strExpr)
        
Dim m As Integer
        
For m = 0 To foundRows.Length - 1
            
'Console.WriteLine(foundRows[i][0]);
            foundRows(m).Delete()
        
Next m

        ShoppingCartDlt.DataSource 
= nowTable2.DefaultView
        ShoppingCartDlt.DataBind()
        Session(
"myCartTable"= nowTable2
        Caculator()
    
End Sub
 'Update 

    
'js
    Sub ShowMsg(ByVal MsgToShow As StringByVal url As String)
        Response.Write(
"<" + "Script language=""JavaScript"">" + Chr(13))
        Response.Write(
"alert('" + MsgToShow + "')" + Chr(13))
        Response.Write(
"window.location='" + url + "'")
        Response.Write(
"</" + "Script>")
    
End Sub


    
Private Sub update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles update.Click
        ShoppingUpdate()
    
End Sub


    
Private Sub CheckOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckOut.Click
        ShoppingUpdate()
        Response.Redirect(
"checkout.aspx")
    
End Sub


    
Private Sub bt_clean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_clean.Click
        Session(
"myCartTable"= Nothing
        Response.Redirect(
"ShoppingCart.aspx")
    
End Sub

End Class


相关图片
购物车功能的现实vb版

你可能感兴趣的:(购物车)