网上购物车简单代码2
chaxun.aspx.csusing 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);
string name = gouwu;
int snum = Convert.ToInt32(gouwu);
if (Session["shopping"] == null)
{
throw new Exception("session_start()无法完成构建任务");
}
else
{
Hashtable ht = (Hashtable)Session["shopping"];
if (gouwu == "mai")
{
if (ht.Contains(id))
{
购物商品 s = (购物商品)ht;
s.Snum = snum;
ht = 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;
}
}
}
页:
[1]