我正在尝试在asp.net中编写一个页面来从Session状态更新用户对象,我遇到了将对象重新置于Session状态的问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
public partial class UpdateAccount : System.Web.UI.Page
{
cAccount account;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["account"] != null)
{
account = (cAccount)Session["account"];
lblID.Text = account.AccountNumber.ToString();
txtEmail.Text = account.Email;
txtFirstName.Text = account.FirstName;
txtLastName.Text = account.LastName;
lblArtist.Text = account.Artist.ToString();
if (account.CardNumber != "")
{
ddlCardType.SelectedValue = account.CardType;
txtNumber.Text = account.CardNumber;
ddlMonth.SelectedIndex = (account.ExpMonth - 1);
txtExpYear.Text = account.ExpYear.ToString();
}
else
{
ddlCardType.SelectedIndex = 0;
txtNumber.Text = "";
ddlMonth.SelectedIndex = 0;
txtExpYear.Text = "";
}
}
else
{
lblID.Text = "-1";
txtEmail.Text = "";
txtFirstName.Text = "";
txtLastName.Text = "";
lblArtist.Text = "";
ddlCardType.SelectedIndex = 0;
txtNumber.Text = "";
ddlMonth.SelectedIndex = 0;
txtExpYear.Text = "";
}
}
protected void btnUpdateUser_Click(object sender, EventArgs e)
{
if (txtOldPass.Text == account.Password)
{
lblOldPass.Text = "";
account.setDetails(txtEmail.Text, txtFirstName.Text, txtLastName.Text);
Session["account"] = account;
Response.Redirect("Accounts.aspx");
}
else
{
lblOldPass.Text = "Password Incorrect";
}
}
}
这是我在加载和更新时使用的代码。这些是使用会话状态的唯一点。
修改
我得到的确切问题是更新的对象不会保存到会话状态。旧的页面仍然在此页面的页面加载和我用于显示对象的页面上。 Form_Load方法在两者中的工作方式相同。
答案 0 :(得分:0)
看起来这里发生的事情是:
用户导航到页面:
用户填写表单详细信息,单击更新用户按钮:
因此,click事件处理程序没有任何问题,但Page_Load活动需要更好地处理回发,即:
if (!Page.IsPostBack)
{
if (Session["account"] != null)
{
.... the rest of your stuff here ....
}
}