using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
namespace ShopCarTest
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox nametb;
protected System.Web.UI.WebControls.TextBox idtb;
protected System.Web.UI.WebControls.TextBox timetbx;
protected System.Web.UI.WebControls.Button addbtn;
protected System.Web.UI.WebControls.Button clearbtn;
protected System.Web.UI.WebControls.Button delbtn;
protected System.Web.UI.WebControls.TextBox idid;
protected System.Web.UI.WebControls.Label msg;
protected System.Web.UI.WebControls.Label carlst;
private void Page_Load(object sender, System.EventArgs e)
{
this.timetbx.Text=DateTime.Now.ToLongDateString();//绑定时间
if(Page.IsPostBack)
{
show();//显示物品
}
}
/// <summary>
/// Store类
/// </summary>
public class store
{
private string name;
private int id;
private DateTime nowtime;
public store(string name,int id,DateTime nowtime)
{
this.name=name;
this.id=id;
this.nowtime=nowtime;
}
public string Name
{
get{return this.name;}
}
public int Id
{
get{return this.id;}
}
public DateTime Nowtime
{
get{return this.nowtime;}
}
}
/// <summary>
/// storelist类
/// </summary>
public class storelist
{
ArrayList st = new ArrayList();
public storelist()
{}
public void add(store myst)
{
st.Add(myst);
}
public ArrayList St
{
get {return this.st;}
}
public store this[int Index]//索引器
{
get {return (store)this.st[Index];}
}
public int count
{
get{return this.st.Count;}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.addbtn.Click += new System.EventHandler(this.addbtn_Click);
this.clearbtn.Click += new System.EventHandler(this.clearbtn_Click);
this.delbtn.Click += new System.EventHandler(this.delbtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void addbtn_Click(object sender, System.EventArgs e)
{
//这个try其实是不需要的,可以在客户端判断输入框的值类型
try
{
//创建购物车
store al = new store(this.nametb.Text.Trim(),int.Parse(this.idtb.Text.Trim()),DateTime.Parse(timetbx.Text.Trim()));
if(Session["stall"]==null)
{
storelist sl = new storelist();
Session["stall"]=sl;
}
//将Session的值赋给数组
storelist ssl =(storelist)Session["stall"];
ssl.add(al);//添加
show();
}
catch
{
this.idtb.Text="请输入数字";
}
}
public void show()
{
//显示购物车的内容
StringBuilder sb = new StringBuilder();
if(Session["stall"]!=null)
{
storelist ssl =(storelist)Session["stall"];
if(ssl.count!=0)
{
for(int i=0;i<ssl.count;i++)
{
sb.Append(i+" "+ssl[i].Name.ToString()+" "+ssl[i].Id.ToString()+" "+ssl[i].Nowtime.ToString()+"<br>");
this.carlst.Text=sb.ToString();
}
}
this.carlst.Text += "总数:"+ssl.count.ToString();
}
}
private void clearbtn_Click(object sender, System.EventArgs e)
{
if(Session["stall"]!=null)
{
this.carlst.Text = "";
// StringBuilder sb = new StringBuilder();
// this.carlst.Visible=false;
Session.Clear();
}
}
private void delbtn_Click(object sender, System.EventArgs e)
{
//这个try和上面的同理
try
{
storelist sls = (storelist)Session["stall"];//将Session值赋给数组
if(this.carlst.Text.Trim().Replace(" ","") != string.Empty && this.idid.Text.Trim() !=string.Empty)
{
if(sls.count != int.Parse("1"))
{
for(int i=0 ;i<sls.count;i++)//取出数组的内容,并且删除
{
if(sls[i] == sls[int.Parse(this.idid.Text.Trim())])
{
sls.St.RemoveAt(i);
}
}
}
else//数组长度为1
{
Session.Clear();
this.carlst.Text="";
}
show();
}
else
{
msg.Text="我靠,都没有东西删鸟啊!";
msg.ForeColor = Color.Red;
}
}
catch
{
this.idid.Text="请输入行号";
}
}
}
}