代码如下:
这个是类文件:
using System;
using System.Collections;
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 class cart
{
private ArrayList items;
public cart()
{
items=new ArrayList();
}
//添加商品到购物车中
public void additems(string item)
{
items.Add(item);
}
//从购物车中删除商品
public void moveitems(string item)
{
items.Remove(item);
}
//查看购物车中的商品
public ArrayList getitems()
{
return items;
}
}
选书界面:
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
{
protected void Page_Load(object sender, EventArgs e)
{
string[] books = new string[] { "qw", "rt", "yu", "yu", "fg", "a", "sadf", "ghj", "fghgh" };
this.ListBox1.DataSource = books;
this.ListBox1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
cart mycart;
if (Session["cart"] == null)
{
mycart = new cart();
Session["cart"] = mycart;
}
mycart = (cart)Session["cart"];
//向购物车中增加书本
mycart.additems(listbox1.selectItem.text);
Session["cart"]=mycart;
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx");
}
}
购物车界面:
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
cart mycart;
if (Session["cart"] == null)
{
mycart = new cart();
Session["cart"] = mycart;
}
else
{
mycart = (cart)Session["cart"];
ListBox2.DataSource = mycart.getitems();
ListBox2.DataBind();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
cart mycart;
if (Session["cart"] == null)
{
mycart = new cart();
Session["cart"] = mycart;
}
else
{
mycart = (cart)Session["cart"];
mycart.moveitems(listbox1.selectItem.Text);
ListBox2.DataSource = mycart.getitems();
ListBox2.DataBind();
}
}
}
今天又改了下,“listbox1.selectItem.Text”这个地方老是报错,如果用“listbox1.Items.ToString()”就可以实现功能,但是显示出来的却是“ System.Web.UI.WebControls”,listbox的属性不是很了解,望高手帮忙解决下,谢谢
[
本帖最后由 搜寻者 于 2009-11-3 22:48 编辑 ]