简阳人论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 1193|回复: 0

网上购物车简单代码2

[复制链接]
发表于 2009-1-6 23:14:49 | 显示全部楼层 |阅读模式 来自 中国北京
chaxun.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class chaxun : System.Web.UI.Page,ICallbackEventHandler
{
    private string output;
    protected void Page_Load(object sender, EventArgs e)
    {
        String ChReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "receive", "context");
        string CallBackScript = "function CallServer(arg,context){" + ChReference + "};";//由于GetCallBackEventRereference创建的函数无法知道名字,所以使用CallServer函数
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myjs", CallBackScript, true);
    }
    public string GetCallbackResult()
    {
        return output;
    }

    public void RaiseCallbackEvent(string eventArgument)
    {
          try
            {//完成购物车:
                String[] gouwu = eventArgument.Split(new String[] { "+++" }, StringSplitOptions.None);         
                if (gouwu.Length > 0 && gouwu.Length <= 4)
                {
                    int id = Convert.ToInt32(gouwu[1]);
                    string name = gouwu[2];
                    int snum = Convert.ToInt32(gouwu[3]);
                    if (Session["shopping"] == null)
                        {
                            throw new Exception("session_start()无法完成构建任务");
                        }
                        else
                        {
                            Hashtable ht = (Hashtable)Session["shopping"];
                            if (gouwu[0] == "mai")
                            {
                                if (ht.Contains(id))
                                {
                                    购物商品 s = (购物商品)ht[id];
                                    s.Snum = snum;
                                    ht[id] = s;
                                }
                                else
                                {
                                    throw new Exception("购物车无法完成数据的存放");
                                }
                                Session["shopping"] = ht;
                                this.output = "mai+++" + id + "+++商品:" + name + "修改成功<br>新数量为:+++" + snum + "+++件";
                            }
                            else
                            {//删除
                                if (ht.Contains(id))
                                {
                                    ht.Remove(id);
                                    Session["shopping"] = ht;
                                    this.output = "del+++" + id + "+++商品:" + name + "删除成功";
                                }
                                else
                                {
                                    throw new Exception("购物车无法完成数据的删除");
                                }
                            }
                          }
                    }
                else
                {
                    this.output = "错误的数据传递";
                }
            }
            catch (Exception e)
            {
                this.output = e.Message;
            }
    }
}
*滑块验证:
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|小黑屋|手机版|Archiver|简阳人论坛 ( 蜀ICP备07502275号-3 )

川公网安备 51200002000214号

GMT+8, 2024-5-15 03:19 , Processed in 0.364517 second(s), 10 queries , Gzip On, File On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表