当前位置:编程学习 > JAVA >>

ASP .NET 四、asp.net+cookie+javascrip制作的购物车

优点:javascrip制作的购物车的加入,可以大大减少对服务器的消耗。
(一)先制作购物介面
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="newshopcar1.aspx.cs" Inherits="newshopcar1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    <style type="text/css">
        #Text_num
        {
            width: 25px;
        }
    </style>
<script language="javascript" type="text/javascript">
// <!CDATA[

function btn_addcar_onclick(name) {
    var value =0;
    if(document.getElementsByName(name)[0].value>0)
    {
    value = document.getElementsByName(name)[0].value;
   
    setCookie(name, value,10);
    //alert(getCookie(name));
    }
}

//去除空格,回车
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}


//设置Cookie
function setCookie(c_name, c_value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);

    document.cookie = c_name + "=" + escape(c_value) +
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

//按名字读取Cookie
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        var m_car=document.cookie.split(';');
        var value=-1;
        for (i = 0; i < m_car.length; i++)
        {
            if(c_name.toString().Trim()==((m_car[i].split('='))[0]).toString().Trim())
            {
                value = (m_car[i].split('='))[1];
            }
           
        }
    }
    return value;
}

// ]]>
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="4" AlternatingItemStyle-Wrap="true" ItemStyle-Width="100">
        <ItemTemplate>
            <asp:Image ID="Image2" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"product_pic") %>' /><br>
            <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"product_name") %>'></asp:Label><br>
            <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"product_price") %>'></asp:Label><br>
            <asp:Label ID="Label3" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem,"product_id") %>'></asp:Label><br>
            <input id="Text_num" type="text" value="1" name='<%# DataBinder.Eval(Container.DataItem,"product_id") %>' maxlength="3"/>件 <br>
            <a href="javascript:void(0)" onclick="return btn_addcar_onclick('<%# DataBinder.Eval(Container.DataItem,"product_id") %>')">加入购物车</a>
        </ItemTemplate>
        </asp:DataList><br>
    </form>
</body>
</html>
 
制作C#代码部份,你会发现,因为javascript的加入,C#的代码变少了
 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class newshopcar1 : System.Web.UI.Page
{
    database db = new database();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.DataList1.DataSource = db.select_db("select * from product_list");
            this.DataList1.DataBind(); //Convert.ToString();
        }
    }
}
 
(二)制做购物车页面
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="newshopcar2.aspx.cs" Inherits="newshopcar2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
<script language="javascript" type="text/javascript">
// <!CDATA[

//删除Cookie并删除相应表格行
function btn_delcar_onclick(name,obj) {
    deleteCookie(name);//删除cookie
    delIndex(obj);//删除表格中的行
}

 //删除cookie
function deleteCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);  
    document.cookie = name + "=''" + "; expires="+ exp.toGMTString();  
    //alert(document.cookie.toString());
}

//删除表格中的行
function delIndex(obj)
{
    var rowIndex = obj.parentNode.parentNode.rowIndex;//获得行下标
  

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,