简阳人论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 1033|回复: 0

网上购物车简单代码4

[复制链接]
发表于 2009-1-6 23:17:18 | 显示全部楼层 |阅读模式 来自 中国北京
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;
using System.Collections;
public partial class _Default : System.Web.UI.Page,ICallbackEventHandler
{
    private SqlConnection conn;

    public SqlConnection Conn
    {
        get { return conn; }
    }
    private int hangNum;

    public int HangNum
    {//总行数
        get { return hangNum; }

    }
    private int pagecount;

    public int Pagecount
    {//每页显示的行数。
        get { return pagecount; }
    }
    private string output;
    protected void Page_Load(object sender, EventArgs e)
    {
        String connpath = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
        pagecount = Convert.ToInt32(ConfigurationManager.AppSettings["pagecount"]);
        conn = new SqlConnection(connpath);
        conn.Open();
        SqlCommand command = conn.CreateCommand();
        command.CommandText = "select count(*) from kucun";
        hangNum = (int)command.ExecuteScalar();
        conn.Close();

        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)
    {
            if (eventArgument.IndexOf("+++") == -1)
            {//没有以这种方式,使用window.open完成查看购物车的效果。
                //Hashtable ht = (Hashtable)Session["shopping"];
                //ICollection ic= ht.Keys;
                //string output = "<table><tr><td></td><td></td></tr>";
                //foreach (String key in ic)
                //{
                //    购物商品 s = (购物商品)ht[key];

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

本版积分规则

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

川公网安备 51200002000214号

GMT+8, 2024-5-14 13:13 , Processed in 0.329566 second(s), 9 queries , Gzip On, File On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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