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

this is simple code:

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

  • manually by changing the value to "Not Original".

  • pressing F5. line:

    txt.Text = "Original";

runs, but the value of Input remains "Not Original"

but when i hit the enter button in the address bar. the value changes to "Original."

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

then, even when I hit the address bar, the value will remain "Not Original"

+4
source share
4 answers

When you refresh the ASP.NET (ASP) page, it will repeat the last action that was performed. So, in your case, if the last thing you did was change the value of the text field, the update will update the text field to this value.

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

"Return" means that you are sending (sending) the page back to yourself. The first time the page loads, IsPostBack is false, because you just request the page - you donโ€™t send anything. But every action you take on an ASP.NET page โ€” once you're there โ€” is a hidden form on the same page. IsPostBack is true for subsequent requests.

Finally, when there is a โ€œ#โ€ in your address bar, clicking on this URL will not reload the page. This is because # means an anchor. If you are already on "page x" and try to go to "page x # something", the page does not reload - it will remain as in the browser, most of the jump to the anchor point, but will not reload.

+12
source

I really noticed the difference in IE (7) and Firefox (3.5) yesterday! I created a form in html with some input fields and with IE updated so that all feilds are returned to empty (default state), but in FireFox the update reloaded the page (including the code changes that I made), but retained the values โ€‹โ€‹that I wrote in the margins! Very useful for development / testing, so I donโ€™t need to rewrite my test data every time!

Just for curiosity, I wrote a simple test in ASP.NET just now, and I noticed the same thing you do for Firefox: pressing the refresh button saves the changed value, but pressing Enter in the address bar reloads the original text in Page_Load . But in IE, the reset value is equal to the original in both cases!

As for the main (browser-independent) differences between different cases, the answer from Rex M is very good.

+2
source

Which browser are you using? Some browsers and plugins try to save form settings under different circumstances.

+1
source

When you refresh the ASP.NET (ASP) page, it will repeat the last action that was performed. Therefore, in your case, if the last thing you did was change the value of the text field, the update will update the text field to this value.

No, this will not happen if you did not dismiss the submit form after you changed the value in your text box.

-1
source

All Articles