Asp.Net - page refresh(F5) do not restore the initial value of TextBox

ari picture ari · Sep 4, 2009 · Viewed 8.8k times · Source

this is the simple code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        txt.Text = "Original";
    }
}
  1. first load. text box state is "Original".

  2. manually , changing the value to "Not Original".

  3. pressing F5. the line:

    txt.Text = "Original";

is executed but the Input value remain "Not Original"

but, when I hit with enter into the adressbar. the value is changed to the "Original".

more starnge is when the adress contains '#' at the end (using jquery click..)

then, even when I hit in the adressbar, the value remain "Not Original"

Answer

Rex M picture Rex M · Sep 4, 2009

When you refresh (F5) an ASP.NET page, it will repeat the last action that was taken. So in your case, if the last thing you did was change a textbox value, refreshing will set the textbox to that value again.

Hitting "enter" in the address bar, however, instructs your browser to discard everything and go to the page completely fresh and new.

"Postback" means you are submitting (posting) the page back to itself. The first time you load a page, IsPostBack is false because you're simply requesting the page - not submitting anything. But every action you take on the ASP.NET page - once you're there - is submitting a hidden form to the same page. IsPostBack is true for those subsequent requests.

Finally, when there is a "#" in your address bar, hitting enter on that URL will not cause the page to reload. This is because the # signifies an anchor. If you are already on "page x" and try to navigate to "page x#something", the page will not reload - it will stay as is in the browser, at most jump to the anchor point, but will not be reloaded.