|
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;
}
}
}
} |
|