如何用listview+cookie编写购物车

来源:互联网 发布:小学 考试软件 编辑:程序博客网 时间:2024/05/16 10:35
最近在做一个图书销售系统的练习,找了很多有关cookie的使用和代码。下面是我用listview+cookie编写的一个购物车:
//前台代码 删除,修改数量的增和减    <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>    <script src="../Scripts/jquery-1.2.6.pack.js" type="text/javascript"></script>    <script type="text/javascript">        function delect(v) {            if (confirm('是否删除此商品?')) {                $("#textshanchu").val(v);                document.getElementById("bthDelect").click();            }        }        function minus(v, v2) {            var n = $(v).next("input").val();            n--;            if (n <=0) {                n = 1;            }            $(v).next("input").val(n);            var p = $(v).parent("td").prev("td").children("span").html();            $(v).parent("td").next("td").children("span").html(n * p);            $("#textxiugai").val(v2);            document.getElementById("btnminus").click();        }        function plus(v, v2) {            var n = $(v).prev("input").val();            n++;            $(v).prev("input").val(n);            var p = $(v).parent("td").prev("td").children("span").html();            $(v).parent("td").next("td").children("span").html(n * p);            $("#textxiugai").val(v2);            document.getElementById("bthplus").click();        }        function change(v, v2) {            var regex = new RegExp("^(?:[1-9]|[1-9]+[0-9])$");            if (!regex.test(v.value)) {                v.value = 1;            }            var p = $(v).parent("td").prev("td").children("span").html();            $(v).parent("td").next("td").children("span").html($(v).val() * p);            document.getElementById("txtgeshu").value = $(v).val();            $("#textxiugai").val(v2);            document.getElementById("butchange").click();        }    </script>//listiview  <asp:ListView ID="ListView1" runat="server"  >             <ItemTemplate>               <table cellpadding="2" width="1500px" border="0" id="tbl1" runat="server" style="font-size:18px;">                        <tr id="Tr2" runat="server">                        <td   width="22%">                            <img width="100" src='<%# Eval("goodsImage") %>' alt="" border="0" />                            </td>                            <td  width="7%">                                <asp:Label ID="goodsname" runat="server" Text='<%# Eval("goodsName") %>' />                            </td>                                                       <td width="22%" >                                <a onclick="minus(this,'<%# Eval("goodsId")%>')">                                    <img src="../图书背景/bag_close.gif" style="width: 24px; height: 24px;" /></a>                                <input type="text" value='<%# Eval("goodsNum")%>' style="text-align: center; width: 30px;height: 24px;"                                    onkeyup="change(this,'<%# Eval("goodsId")%>')" />                                <a onclick="plus(this,'<%# Eval("goodsId")%>')">                                    <img src="../图书背景/bag_open.gif" style="width: 24px; height: 24px;" /></a>                            </td>                             <td width="6%">                                <asp:Label ID="goodsprice" runat="server" Text='<%# Eval("goodsPrice") %>' />                            </td>                             <td width="24%">                                <asp:Label ID="xiaoji" runat="server" Text='<%# Eval("Orders_total") %>'></asp:Label>                            </td>                            <td width="24%">                                <a onclick='delect("<%# Eval("goodsId")%>","Delect")' style="cursor:pointer; text-decoration:underline;">删除</a>                            </td>                        </tr>                        </table>                    </ItemTemplate>            </asp:ListView>//先创建四个button,三个textbox<asp:TextBox ID="textshanchu" runat="server"   style="display:none;"></asp:TextBox>    <asp:TextBox ID="textxiugai" runat="server" style="display:none;"></asp:TextBox>    <asp:TextBox ID="txtgeshu" runat="server" style="display:none;"></asp:TextBox>    <asp:Button ID="bthDelect" runat="server" Text="Button" onclick="bthDelect_Click"   style="display:none;"/>    <asp:Button ID="butchange" runat="server" Text="Button" onclick="butchange_Click"  style="display:none;"/>    <asp:Button ID="btnminus" runat="server" Text="Button" onclick="btnminus_Click"  style="display:none;"/>    <asp:Button ID="bthplus" runat="server" Text="Button" onclick="bthplus_Click" style="display:none;" />

//后台using System.Data.SqlClient;using System.Data;public void DataLoad()//在page_load里面调用此函数        {            decimal Total = 0;            DataTable DataTable1 = new DataTable();            DataTable1.Columns.Add("goodsId", typeof(string));            DataTable1.Columns.Add("goodsName", typeof(string));            DataTable1.Columns.Add("Orders_total", typeof(int));            DataTable1.Columns.Add("goodsPrice", typeof(int));            DataTable1.Columns.Add("goodsNum", typeof(int));            DataTable1.Columns.Add("goodsImage", typeof(string));                       //获取cookies                        HttpCookie cookies = Request.Cookies["shoppingcart"];            //如果cookies为空值,则不显示提交订单按钮            if (cookies == null)            {                Msg("您还没有选择商品!");                Panel2.Visible = false;                return;            }            //遍历cookies里面的数据            foreach (string item in Request.Cookies["shoppingcart"].Values)            {                if (item != null)                {                    //新建一个数组存放cookies里面的数据                    string[] w = Request.Cookies["shoppingcart"][item].Split('|');                    //创建新的一行数据                    DataRow newRow = DataTable1.NewRow();                    //为新的一行数据赋值                    newRow["goodsId"] = w[0];                    newRow["goodsName"] = w[1];                    newRow["goodsPrice"] = int.Parse(w[3].ToString());                    newRow["goodsNum"] = int.Parse(w[4].ToString());                    newRow["goodsImage"] = w[5];                    newRow["Orders_total"] = int.Parse(newRow["goodsPrice"].ToString()) * int.Parse(newRow["goodsNum"].ToString());                    //添加到DataTable中                    DataTable1.Rows.Add(newRow);                }            }            //遍历DataTable,为total变量赋值            for (int i = 0; i < DataTable1.Rows.Count; i++)            {                Total += decimal.Parse(DataTable1.Rows[i]["goodsPrice"].ToString()) * decimal.Parse(DataTable1.Rows[i]["goodsNum"].ToString());            }            labtotal.Text = Total.ToString();            ListView1.DataSource = DataTable1;            ListView1.DataBind();            if (ListView1.Items.Count == 0)            {                Panel2.Visible = false;                Response.Write("<script>alert('您没有选择商品!');location='Homepage.aspx'</script>");            }        }protected void bthplus_Click(object sender, EventArgs e) //加        {            //获取cookies            HttpCookie cookies = Request.Cookies["shoppingcart"];            if (cookies != null)            {                //找到对应管理号的数据                string s = cookies[textxiugai.Text.Trim()];                //新建一个数组,存放对应管理号的商品信息                string[] str = s.Split('|');                //新建一个变量,存放改动信息                string t = textxiugai.Text.Trim() + "|" + str[1] + "|" + str[2] + "|" + str[3] + "|" + (int.Parse(str[4]) + 1).ToString()+"|"+str[5];                //修改对应管理号的数据                cookies.Values.Set(textxiugai.Text.Trim(), t);            }            //追加到cookies中            Response.AppendCookie(cookies);            DataLoad();        }        protected void btnminus_Click(object sender, EventArgs e) //减        {                       //获取cookies            HttpCookie cookies = Request.Cookies["shoppingcart"];            if (cookies != null)            {                //找到对应管理号的数据                string s = cookies[textxiugai.Text.Trim()];                //新建一个数组,存放对应管理号的商品信息                string[] str = s.Split('|');                //控制个数不能小于1                int i = 1;                if (int.Parse(str[4]) - 1 < 1)                {                    str[4] = i.ToString();                }                else                {                    //新建一个变量,存放改动信息                    string t = textxiugai.Text.Trim() + "|" + str[1] + "|" + str[2] + "|" + str[3] + "|" + (int.Parse(str[4]) - 1).ToString() + "|" + str[5];                    //修改对应管理号的数据                    cookies.Values.Set(textxiugai.Text.Trim(), t);                }            }            //追加到cookies中            Response.AppendCookie(cookies);        }        protected void butchange_Click(object sender, EventArgs e) //更改数量        {            //获取cookies            HttpCookie cookies = Request.Cookies["shoppingcart"];            if (cookies != null)            {                //找到对应管理号的数据                string s = cookies[textxiugai.Text.Trim()];                //新建一个数组,存放对应管理号的商品信息                string[] str = s.Split('|');                //新建一个变量,存放改动信息                string t = textxiugai.Text.Trim() + "|" + str[1] + "|" + str[2] + "|" + str[3] + "|" + txtgeshu.Text.Trim()+"|"+str[5];                //修改对应管理号的数据                cookies.Values.Set(textxiugai.Text.Trim(), t);            }            //追加到cookies中            Response.AppendCookie(cookies);            DataLoad();        }        protected void bthDelect_Click(object sender, EventArgs e) //删除        {            //获取cookies              HttpCookie cookies = Request.Cookies["shoppingcart"];            //遍历cookies            foreach (object obj in cookies.Values)            {                //判断是否是要删除的值                if (obj.ToString() == textshanchu.Text)                {                    //移除对应的管理号的数据                    cookies.Values.Remove(textshanchu.Text);                    //追加到cookies中                    Response.AppendCookie(cookies);                    break;                }            }            DataLoad();        }

原创粉丝点击