zoukankan      html  css  js  c++  java
  • 实现购物车详细代码vs03

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

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

     26            }

     27            catch
     28            {
     29                Response.Redirect("error.htm");
     30            }

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

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

     45            }

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

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

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

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

     84        }

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

     94        }

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

    112                    else
    113                    {
    114                        i++;
    115                    }

    116                }

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

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

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

    149        }

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

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

    170            }

    171        }

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

    202            }

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

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

    217        Web 窗体设计器生成的代码
    231        private void update_Click(object sender, System.EventArgs e)
    232        {
    233            Update();
    234        }

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

    240    }

    241}
  • 相关阅读:
    Python学习札记(十五) 高级特性1 切片
    LeetCode Longest Substring Without Repeating Characters
    Python学习札记(十四) Function4 递归函数 & Hanoi Tower
    single number和变体
    tusen 刷题
    实验室网站
    leetcode 76. Minimum Window Substring
    leetcode 4. Median of Two Sorted Arrays
    leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions 、434. Number of Islands II(lintcode) 并查集 、178. Graph Valid Tree(lintcode)
    刷题注意事项
  • 原文地址:https://www.cnblogs.com/zwl12549/p/608915.html
Copyright © 2011-2022 走看看