我想創建一個對象,然後將它保存在一個Session中,然後重定向到另一個頁面並使用會話中保存的對象。在會話中保存一個已經創建的對象
這是我的代碼,這似乎是錯誤的地方,因爲它不可操作。
PAGE 1
public FitnessClassOpportunity GetData()
{
return new FitnessClassOpportunity(txtId.Text, txtDescription.Text, txtLocation.Text,
Convert.ToInt32(tx2.Text), dropDownDay.SelectedItem.ToString(),
txtTime.Text, Convert.ToInt32(txtDuration.Text), CheckBox1.Checked,
txtDatecompleted.Text, txtNumSession.Text);
}
protected void Button1_Click(object sender, EventArgs e)
{
//result from breaking point: f has the data
FitnessClassOpportunity f = GetData();
//result from breaking point: f still has the data but Session is still NULL
Session["object"] = f;
Response.Redirect("Default.aspx");
}
PAGE 2
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
}
else
{
//add the object from the session to a list
fitnessClassList.addFitnessClass((FitnessClassOpportunity)Session["object"]);
UpdateListbox();
}
}
什麼是'fitnessClassList'和'addFitnessClass'是什麼?你有任何錯誤? – 2012-03-27 20:42:23
你確定重定向被認爲是回發嗎?我的猜測是,代碼根本不會被執行。 – 2012-03-27 20:43:35
我想你應該將addFitnessClass&UpdateListbox代碼移動到if的_then_分支。 – 2012-03-27 20:44:41