Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"

like image 913
ari Avatar asked Dec 10 '22 19:12

ari


2 Answers

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.

like image 158
Rex M Avatar answered May 12 '23 06:05

Rex M


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.

like image 29
awe Avatar answered May 12 '23 06:05

awe