I am trying to write a page in asp.net to update an user object from Session state and I am running into a problem putting the object back into the Session state
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";
}
}
}
This is the code i am using on load, and on update. These are the only spots session state is used.
EDIT
The exact problem I am getting is the updated object does not get saved to the Session State. The old one remains on page load of this page and the page I am using to display the object. The Form_Load method works the same in both.
It looks like what is happening here is:
User navigates to page:
User fills in form details, clicks the update user button:
So there’s nothing wrong the click event handler, but the Page_Load activity needs to handle the post back better, i.e.: