Session variable has old value, needs to update with page redirect

I have session variable that dont update with new value. I have two pages, one were you enter the values and klick on the button and you get redirected to page 2 and there you can check your input, if this is wrong you click back-button and you go back to the first page where you can change the input but now when i click the button to validate again the new value does not show up in the session variable but only the old value. I have been readingabout session for the last day but i just cant find the problem, the behind code is below:

Page1

protected void Page_Load(object sender, EventArgs e)
{
    if (this.Session["value1"] != null)
    {
        lbl1.Text = (String)this.Session["value1"].ToString();
    }
}   

public string info { get { return lbl1.Text; } }

protected void inputButton_onclick(object sender, EventArgs e)
{
    Page.Validate();

    if (Page.IsValid)
    {
        Session["value1"] = info;

        Response.Redirect("~/validpage.aspx");
    }

}

Page 2

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["value1"] != null)
    {   
        lbl2.Text = (String)Session["value1"].ToString();
    }        
}

protected void BackButton_Click(object sender, EventArgs e)
{
    Session["value1"] = lbl2.Text;

    Response.Redirect("~/Default.aspx");
}

Maybe i have staired my self blind on this code as to me this should not have this problem it is presenting. Any idea and help will be appreciated.

Answers


Every time Page1 loads, lbl1 is set to the contents of the session, unless it's never been set. So when you click the button, the lbl1 is first set back to the content of the session as the page is loaded. You then read this value back & but it back in the session.

try this instead:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostback)
    {
        if (this.Session["value1"] != null)
        {
            lbl1.Text = (String)this.Session["value1"].ToString();
        }
    }
}   

This says only set the value if we're not postback, ie NOT clicking a button on the page.

Also in page2, there's no need to call ToString AND cast to a string. Do either, not both.


Need Your Help

Why my Aspect is not detected for Jersey controller (using custom annotation)?

java jersey aop aspectj spring-aop

I want to create an Aspect over a Jersey controller to measure how long the services take to be executed. I'm fighting against my pointcut since it isn't detected and my aspect never gets launched....

Samung S-Pen detection in cordova app

android cordova ionic easeljs stylus-pen

I am developing a cordova app with ionic. I have some drawing functionality using canvas. Is there any way to detect whether user is drawing with finger or a S-Pen(Stylus) provided by samsung with ...