Shopping类
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.Collections;
/// <summary>
/// Shopping 的摘要说明
/// </summary>
public class Shopping
{
//购物车
public Hashtable SelectedGoods = new Hashtable();
public Shopping()
{
}
}
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.Collections;
/// <summary>
/// Shopping 的摘要说明
/// </summary>
public class Shopping
{
//购物车
public Hashtable SelectedGoods = new Hashtable();
public Shopping()
{
}
}
BuyBook.aspx
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;
public partial class _Default : System.Web.UI.Page
{
private Shopping obj;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["goods"]==null)
{
obj = new Shopping();
Session["goods"] = obj;
}
else
{
obj = Session["goods"] as Shopping;
}
if (!IsPostBack)
{
ShowSelectedGoods();//显示被选中的商品
}
}
protected void btnShoppingGoods_Click(object sender, EventArgs e)
{
SaveSelectedGoods();//保存被选中的商品
Response.Redirect("ShowGoods.aspx");
}
protected void btnNextBuyComputer_Click(object sender, EventArgs e)
{
SaveSelectedGoods();
Response.Redirect("BuyComputer.aspx");
}
private void SaveSelectedGoods()
{
CheckBox chk;
//循环检查位于窗体上的所有控件
foreach (Control ctl in form1.Controls)
{
//如果控件类型名为“CheckBox”
if (ctl.GetType().Name == "CheckBox")
{
chk = ctl as CheckBox;
//检查一下购物车中是否已有此书了
if (obj.SelectedGoods.ContainsKey("book:" + chk.ID))
{
if (chk.Checked == false)
//从购物车中移除此书
obj.SelectedGoods.Remove("book:" + chk.ID);
}
else //购物车中没有此书
if (chk.Checked) //复选框选中
//向购物车中添加此书
obj.SelectedGoods.Add("book:" + chk.ID, chk.Text);
}
}
//将购物车对象保存到session中
Session["goods"] = obj;
}
private void ShowSelectedGoods()
{
foreach (Control ctl in form1.Controls)
if (ctl.GetType().Name == "CheckBox")
(ctl as CheckBox).Checked = false;
foreach (Object key in obj.SelectedGoods.Keys)
{
if (key.ToString().IndexOf("book:") != -1)
{
string id = key.ToString().Substring(key.ToString().LastIndexOf(":") + 1);
Control ctl = form1.FindControl(id);
if (ctl != null)
(ctl as CheckBox).Checked = true;
}
}
}
}
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;
public partial class _Default : System.Web.UI.Page
{
private Shopping obj;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["goods"]==null)
{
obj = new Shopping();
Session["goods"] = obj;
}
else
{
obj = Session["goods"] as Shopping;
}
if (!IsPostBack)
{
ShowSelectedGoods();//显示被选中的商品
}
}
protected void btnShoppingGoods_Click(object sender, EventArgs e)
{
SaveSelectedGoods();//保存被选中的商品
Response.Redirect("ShowGoods.aspx");
}
protected void btnNextBuyComputer_Click(object sender, EventArgs e)
{
SaveSelectedGoods();
Response.Redirect("BuyComputer.aspx");
}
private void SaveSelectedGoods()
{
CheckBox chk;
//循环检查位于窗体上的所有控件
foreach (Control ctl in form1.Controls)
{
//如果控件类型名为“CheckBox”
if (ctl.GetType().Name == "CheckBox")
{
chk = ctl as CheckBox;
//检查一下购物车中是否已有此书了
if (obj.SelectedGoods.ContainsKey("book:" + chk.ID))
{
if (chk.Checked == false)
//从购物车中移除此书
obj.SelectedGoods.Remove("book:" + chk.ID);
}
else //购物车中没有此书
if (chk.Checked) //复选框选中
//向购物车中添加此书
obj.SelectedGoods.Add("book:" + chk.ID, chk.Text);
}
}
//将购物车对象保存到session中
Session["goods"] = obj;
}
private void ShowSelectedGoods()
{
foreach (Control ctl in form1.Controls)
if (ctl.GetType().Name == "CheckBox")
(ctl as CheckBox).Checked = false;
foreach (Object key in obj.SelectedGoods.Keys)
{
if (key.ToString().IndexOf("book:") != -1)
{
string id = key.ToString().Substring(key.ToString().LastIndexOf(":") + 1);
Control ctl = form1.FindControl(id);
if (ctl != null)
(ctl as CheckBox).Checked = true;
}
}
}
}
BuyComputer.aspx
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 BuyComputer : System.Web.UI.Page
{
private Shopping obj;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["goods"] == null)
{
obj = new Shopping();
Session["goods"] = obj;
}
else
{
obj = Session["goods"] as Shopping;
}
if (!IsPostBack)
{
ShowSelectedArticles();
}
}
protected void btnBuyBook_Click(object sender, EventArgs e)
{
SaveSelectedArticles();
Response.Redirect("BuyBook.aspx");
}
private void SaveSelectedArticles()
{
CheckBox chk;
//循环检查位于窗体上的所有控件
foreach (Control ctl in form1.Controls)
{
//如果控件类型名为“CheckBox”
if (ctl.GetType().Name == "CheckBox")
{
chk = ctl as CheckBox;
//检查一下购物车中是否已有此电脑商品了
if (obj.SelectedGoods.ContainsKey("computer:" + chk.ID))
{
if (chk.Checked == false)
//从购物车中移除此电脑商品
obj.SelectedGoods.Remove("computer:" + chk.ID);
}
else //购物车中没有此电脑商品
if (chk.Checked) //复选框选中
//向购物车中添加此电脑商品
obj.SelectedGoods.Add("computer:" + chk.ID, chk.Text);
}
}
//将购物车对象保存到session中
Session["articles"] = obj;
}
private void ShowSelectedArticles()
{
foreach (Control ctl in Controls)
if (ctl.GetType().Name == "CheckBox")
(ctl as CheckBox).Checked = false;
foreach (Object key in obj.SelectedGoods.Keys)
{
if (key.ToString().IndexOf("computer:") != -1)
{
string id = key.ToString().Substring(key.ToString().LastIndexOf(":") + 1);
Control ctl = form1.FindControl(id);
if (ctl != null)
(ctl as CheckBox).Checked = true;
}
}
}
protected void btnShoppingGoods_Click(object sender, EventArgs e)
{
SaveSelectedArticles();
Response.Redirect("ShowGoods.aspx");
}
}
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 BuyComputer : System.Web.UI.Page
{
private Shopping obj;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["goods"] == null)
{
obj = new Shopping();
Session["goods"] = obj;
}
else
{
obj = Session["goods"] as Shopping;
}
if (!IsPostBack)
{
ShowSelectedArticles();
}
}
protected void btnBuyBook_Click(object sender, EventArgs e)
{
SaveSelectedArticles();
Response.Redirect("BuyBook.aspx");
}
private void SaveSelectedArticles()
{
CheckBox chk;
//循环检查位于窗体上的所有控件
foreach (Control ctl in form1.Controls)
{
//如果控件类型名为“CheckBox”
if (ctl.GetType().Name == "CheckBox")
{
chk = ctl as CheckBox;
//检查一下购物车中是否已有此电脑商品了
if (obj.SelectedGoods.ContainsKey("computer:" + chk.ID))
{
if (chk.Checked == false)
//从购物车中移除此电脑商品
obj.SelectedGoods.Remove("computer:" + chk.ID);
}
else //购物车中没有此电脑商品
if (chk.Checked) //复选框选中
//向购物车中添加此电脑商品
obj.SelectedGoods.Add("computer:" + chk.ID, chk.Text);
}
}
//将购物车对象保存到session中
Session["articles"] = obj;
}
private void ShowSelectedArticles()
{
foreach (Control ctl in Controls)
if (ctl.GetType().Name == "CheckBox")
(ctl as CheckBox).Checked = false;
foreach (Object key in obj.SelectedGoods.Keys)
{
if (key.ToString().IndexOf("computer:") != -1)
{
string id = key.ToString().Substring(key.ToString().LastIndexOf(":") + 1);
Control ctl = form1.FindControl(id);
if (ctl != null)
(ctl as CheckBox).Checked = true;
}
}
}
protected void btnShoppingGoods_Click(object sender, EventArgs e)
{
SaveSelectedArticles();
Response.Redirect("ShowGoods.aspx");
}
}
ShowGoods.aspx
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 ShowGoods : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["goods"]==null)
{
Label lblinfo = new Label();
lblinfo.Text = "无";
PlaceHolder1.Controls.Add(lblinfo);
}
else
{
Shopping shop = Session["goods"] as Shopping;
if (shop.SelectedGoods.Count==0)
{
Label lblinfo = new Label();
lblinfo.Text = "无";
PlaceHolder1.Controls.Add(lblinfo);
}
else
{
BulletedList list = new BulletedList();
foreach (object key in shop.SelectedGoods.Keys)
{
list.Items.Add(shop.SelectedGoods[key].ToString());
}
PlaceHolder1.Controls.Add(list);
}
}
}
protected void btnBuyBook_Click(object sender, EventArgs e)
{
Response.Redirect("BuyBook.aspx");
}
protected void btnBuyComputer_Click(object sender, EventArgs e)
{
Response.Redirect("BuyComputer.aspx");
}
}
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 ShowGoods : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["goods"]==null)
{
Label lblinfo = new Label();
lblinfo.Text = "无";
PlaceHolder1.Controls.Add(lblinfo);
}
else
{
Shopping shop = Session["goods"] as Shopping;
if (shop.SelectedGoods.Count==0)
{
Label lblinfo = new Label();
lblinfo.Text = "无";
PlaceHolder1.Controls.Add(lblinfo);
}
else
{
BulletedList list = new BulletedList();
foreach (object key in shop.SelectedGoods.Keys)
{
list.Items.Add(shop.SelectedGoods[key].ToString());
}
PlaceHolder1.Controls.Add(list);
}
}
}
protected void btnBuyBook_Click(object sender, EventArgs e)
{
Response.Redirect("BuyBook.aspx");
}
protected void btnBuyComputer_Click(object sender, EventArgs e)
{
Response.Redirect("BuyComputer.aspx");
}
}