在AJAX中使用Cookie保存购物车的方法
[ASPX]

<%@PageLanguage="C#"MasterPageFile="~/Scripts/ShopCart/ShopCart.master"AutoEventWireup="true"
CodeFile="Default.aspx.cs"Inherits="_Default"Title="购物车"%>


<%@ImportNamespace="AW.Components.Helpers"%>

<asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"runat="Server">
<asp:MultiViewID="MultiView1"runat="server">
<asp:ViewID="View1"runat="server">


<scriptlanguage="javascript"type="text/javascript"src='<%=this.ResolveUrl("~/Utility/Functions.js")%>'></script>


<scriptlanguage="javascript"type="text/javascript">
varkey='<%=Key%>';

functionremove(pID){
varv=getCookie(key)==null?"":getCookie(key);
_Default.RemoveProductFromOrder(pID,v,remove_Callback);
}

functionremove_Callback(res){
if(res.error!=null)alert(res.error);

else{
vartr=document.getElementById("tr_"+res.value.PID.toString());
tr.style.display="none";
deleteCookie(key,"/","."+location.host);
setCookie(key,res.value.NVC,null,"/","."+location.host);
calcMoney();
}
}

functionmodify(pID,num){
varv=getCookie(key)==null?"":getCookie(key);
_Default.ModifyProductNumber(pID,num,v,modify_Callback);
}

functionmodify_Callback(res){
deleteCookie(key,"/","."+location.host);
setCookie(key,res.value.NVC,null,"/","."+location.host);
vartotal=document.getElementById("txtSubTotal_"+res.value.PID.toString());
varprice=document.getElementById("txtVipPrice_"+res.value.PID.toString());
total.value=Round(parseFloat(parseInt(res.value.TOTAL)*parseFloat(price.value)),2);
calcMoney();
}

functionmodifyAll(){

}

functionclearCart(){
if(!confirm('确定要清空购物车吗?'))return;
vartb=document.getElementById("tbOrder");

for(vari=1;i<tb.rows.length;i++){
vartr=tb.rows.item(i);

if(tr.id.indexOf("_")!=-1&&tr.style.display!="none"){
tr.style.display='none';
}
}
deleteCookie(key,"/","."+location.host);
varctlTotalNum=document.getElementById("<%=lbTotalNum.ClientID%>");
varctlTotalMoney=document.getElementById("<%=lbTotalMoney.ClientID%>");
varctlDiscount=document.getElementById("<%=lbDiscount.ClientID%>");
ctlTotalNum.innerHTML="0";
ctlTotalMoney.innerHTML="0";
ctlDiscount.innerHTML="0";
}


functioncalcMoney(){
varctlNums=document.getElementsByName("txtNumber");
varctlPrices=document.getElementsByName("txtPrice");
varctlVipPrices=document.getElementsByName("txtVipPrice");
varctlSubTotal=document.getElementsByName("txtSubTotal");
varctlTotalNum=document.getElementById("<%=lbTotalNum.ClientID%>");
varctlTotalMoney=document.getElementById("<%=lbTotalMoney.ClientID%>");
varctlDiscount=document.getElementById("<%=lbDiscount.ClientID%>");
vartotalNum=0;
varsubTotal=0;
varsubTotalVip=0;
vartotalMoney=0;
vartotalVipMoney=0;


for(vari=0;i<ctlNums.length;i++){
totalNum+=parseInt(ctlNums[i].value);
subTotal=parseFloat(totalNum*parseFloat(ctlPrices[i].value));
subTotalVip=parseFloat(totalNum*parseFloat(ctlVipPrices[i].value));
totalMoney+=subTotal;
totalVipMoney+=subTotalVip;
ctlSubTotal.value=subTotalVip.toString();
}
ctlTotalNum.innerHTML=Round(totalNum,2);
ctlTotalMoney.innerHTML=Round(totalVipMoney,2);
ctlDiscount.innerHTML=Round((totalMoney-totalVipMoney),2);
}

functionRound(a_Num,a_Bit){
return(Math.round(a_Num*Math.pow(10,a_Bit))/Math.pow(10,a_Bit)).toString();
}
</script>

<divid="shopcart">
<tablewidth="778"border="0"align="center"cellpadding="0"cellspacing="0"background="../../Icon/ShopCart/shopC_syp_5.gif">
<tbody>
<tr>
<tdwidth="12">
<imgheight="72"src="../../Icon/ShopCart/shopC_syp_1.gif"width="12"/></td>
<td>
<imgheight="72"src="../../Icon/ShopCart/shopC_6.gif"width="176"/></



















































































































本文地址:http://www.45fan.com/a/question/69592.html