ASP.NET - retain the text box value after the page load

Asked By muthuraman alexander on 14-Dec-11 05:55 AM
hi all
im developing a site (asp.net with c# )
in that i have a registration form
user entered the details like name,address,.... in the text boxes and navigate to the next page to finalise the regitration..
but when user want back to regitration page the previous entered data should retain in those text boxes
what can i do for this......
i tried by adding enable viewstate =true in the text boxes....it was not working\
please advice me

thanks in advance
MUTHU
dipa ahuja replied to muthuraman alexander on 14-Dec-11 06:05 AM
If you are going to the prev page by using the saperate button from the next page, then it will not retain its value.

but it will retain the value if you click on the Browser BACK key. Because when click on the button POSTBACK occurs, and the value gets reset.

One way you can do it by using the session.

protected void Button3_Click(object sender, EventArgs e)
{
  Session["username"] = TextBox1.Text;
  Session["password"] = TextBox2.Text;
  Response.Redirect("filter.aspx");
}

and on previous page

protected void Page_Load(object sender, EventArgs e)
{
  if (Session["username"] != null && Session["password"] != null)
  {
    TextBox1.Text = Session["username"].ToString();
    TextBox2.Text = Session["password"].ToString();
  }
}

But better to take the all registration field on the same page to avoid this
Suchit shah replied to muthuraman alexander on 14-Dec-11 06:39 AM

Put the value in a session or Hidden variable or Query String

and then once ur getting back to page on page load event get the value into the text box.

You can load the textbox value into a session variable.

Session("hold") = Textbox1.text

When you reload the page, reload the textbox with the session variable.

TextBox1.Text = Session("hold")

Venkat K replied to muthuraman alexander on 14-Dec-11 07:13 AM
Have you tried enabling ViewState for the complate page? Normally if you use the browser back button to the previous page then definitely the values inside the form would be preserver.
But there is a new page load happen then there is a chance of disappearing the data in the textboxes.

One soultion would be maintain the textboxes data in session or in any object and retain the values from the object back when the page is being redirected second time.

Thanks
kalpana aparnathi replied to muthuraman alexander on 14-Dec-11 01:37 PM

Hi,

According to your description ,please check the code below,it shows how to keep the old value when the page redirect back. 

1.COde in your Webform1.aspx(.cs):

protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      if (Session["1"] != null)
      {
        this.TextBox1.Text = Session["1"].ToString();
      }
    }
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
    Session["1"] = TextBox1.Text;
    Response.Redirect("Default.aspx");
  }

2.Code in your Default.aspx(.cs):

protected void Page_Load(object sender, EventArgs e)
  {
    if (Session["1"] != null)
    {
      switch (Session["1"].ToString())
      {
        case "1":
          TextBox1.Text = Session["1"].ToString();
          break;
        case "2":
          TextBox2.Text = Session["1"].ToString();
          break;
        default:
          break;
      }
    }
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
    Response.Redirect("WebForm1.aspx");
  }

 

Hope it can help you.