Francis Gall
Francis Gall

Reputation: 77

Asp.net Session state Trying to keep contents of textbox when returned to page

ive two pages, one with a text box and a button, the other with a button a label. What i want to do is to display contents of the textbox on page 1, in the label of the page2 on button click. and then when i click the button to return to page1. preverse whats entered in the textbox on page1. sorry if its confusing. heres my code

page1.aspx

 protected void Button1_Click(object sender, EventArgs e)

 {      
 Session["fstName"] = txtBox.Text;
 Response.Redirect("Page2.aspx");
  }

page2.aspx

protected void Page_Load(object sender, EventArgs e)
    {
        string a = Session["fstName"].ToString();
        lblPage2.Text = a;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("WebForm1.aspx");
    }

Upvotes: 0

Views: 1271

Answers (1)

David
David

Reputation: 218887

Where do you set the value of the text box when returning to WebForm1.aspx? It should be very similar to what you have for the label on Page2.aspx. Something like:

protected void Page_Load(object sender, EventArgs e)
{
    string a = Session["fstName"].ToString();
    txtBox.Text = a;
}

At worst, you may need to wrap some error checking around it. Maybe something like:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        if (Session["fstName"] != null)
        {
            string a = Session["fstName"].ToString();
            txtBox.Text = a;
        }
}

Upvotes: 1

Related Questions