Session保存对象的问题,救命。
郁闷了,我的Session变量死活把一个对象保存不下来,怎么回事。
我在在Global的Session_Start
里初始化;
我把他取出来,,我取他的CartCource 属性返回一个空的string[],我把他的CartCource通过Click副了直的。
这个对象到是每次我用ToString()都是在的,就是他的属性里返回的直不见了。
完整代码如下里面有一些调试信息:
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 SelectCourceSystemDll;
namespace SelectCourceSystem
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button addToCart;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected CourceCart myCourceCart;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if((myCourceCart=(CourceCart)Session["myCourceCart"])!=null)
{
myCourceCart=(CourceCart)Session["myCourceCart"];
}
Label2.Text=myCourceCart.ToString();
if(!IsPostBack)
{
string myID="1";
Cource myCource=new Cource();
DataGrid1.DataSource=myCource.getStudentCource(myID);
DataGrid1.DataBind();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.addToCart.Click += new System.EventHandler(this.addToCart_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.PreRender += new System.EventHandler(this.WebForm1_PreRender);
}
#endregion
private void addToCart_Click(object sender, System.EventArgs e)
{
CheckBox myCheckBox;
myCourceCart.Clear();
ArrayList Cource=new ArrayList();
for(int i=0;i<DataGrid1.Items.Count;i++)
{
myCheckBox=((CheckBox)DataGrid1.Items[i].FindControl("AddToCourceCart"));
if(myCheckBox.Checked==true)
{
Cource.Add(DataGrid1.Items[i].Cells[0].Text.ToString());
}
}
string[] CourceID=new string[Cource.Count];
for(int i=0;i<Cource.Count;i++)
{
CourceID[i]=(string)Cource[i];
}
myCourceCart.AddToCart(CourceID);
string[] tem=myCourceCart.CartCource;
Label1.Text=tem[0];
Session["myCourceCart"]=myCourceCart;
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
string myID="1";
DataGrid1.CurrentPageIndex=e.NewPageIndex;
Cource myCource=new Cource();
DataGrid1.DataSource=myCource.getStudentCource(myID);
DataGrid1.DataBind();
string[] CourceID=myCourceCart.CartCource;
CheckBox myCheckBox;
Label1.Text="hi"+CourceID.Length.ToString();
//myCheckBox=((CheckBox)DataGrid1.Items[i].FindControl("AddToCourceCart"));
//myCheckBox.Checked=true;
for(int i=0;i<DataGrid1.Items.Count;i++)
{
foreach(string Item in CourceID)
{
if(DataGrid1.Items[i].Cells[0].Text.ToString()==Item)
{
myCheckBox=((CheckBox)DataGrid1.Items[i].FindControl("AddToCourceCart"));
myCheckBox.Checked=true;
}
}
}
Label2.Text=myCourceCart.ToString()+"hello";
}
private void WebForm1_PreRender(object sender, System.EventArgs e)
{
}
}
}