views:

1855

answers:

5

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"

+8  A: 

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.

Rex M
What about a hard refresh (CRTL + F5)? Shouldn't that work like a new request?
Eddie
@Eddie CTRL-F5 still repeats the last action, but also tells the browser to update any cached assets.
Rex M
+1  A: 

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

Mike Buckbee
+2  A: 

I actually noticed a difference in IE(7) and Firefox(3.5) yesterday! I created a form in html with some input fields, and using IE, a refresh caused all feilds to be set back to blank (default state) but in FireFox, a refresh reloaded the page (including code changes I had done) but kept the values I had written in the fields! Very useful when developing/testing, so I did not need to rewrite my test data each time!

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

As for the basic (not browser dependent) differences between the different cases, the answer from Rex M is very good.

awe
yes, i've seen this too with firefox. i assume that FF does it to avoid losing what the user has typed in.ari's question doesn't mention anything about posting back the form so i think Rex M may be describing a different scenario.
Matt Sherman
A: 

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.

No it won't, unless you fired a form submit after you changed the value in your textbox.

chris
A: 

excellent answer to this topic, it was very helpfull

lvarela