Praveen Tiwari
Praveen Tiwari

Reputation: 1310

IsPostBack property of the page never gets false even after page refresh

I get stuck in a very unusual problem. I've a code written in C# which is simply checking IsPostBack property on the Page_Load. I know that IsPostBack remains false when page lands for the first time and bocme true only when any control post the form to the server (having runat=sever).

I also know that if we hit refresh, the IsPostBack property should change to false (since refresh is not a postback).

This is the sample code:

if (!IsPostBack)
{
    // If I click on any control on the page, and then hit refresh,
    // the code inside this block should execute, but this is not happening.
    // After first postback, I tried refreshing the page for more than
    // ten times, but always finds IsPostBack=true

    // ...
}
else
{
    // ...
}

I clicked on a server side button (a postback), then hit refresh. I assume it will go to the if block but no luck. No matter how many times i hit Refresh on browser, IsPostBack is always true. which is truly an unusual activity I've never seen before.

I would really appreciate any help. I need to know why this is happening, is this a browser related problem or something else? I used mozilla and chrome.

Every time I hit refresh, I get a warning on both browsers.

Thanks in advance for any kind help.

Praveen

Upvotes: 2

Views: 2569

Answers (2)

bmm6o
bmm6o

Reputation: 6515

Those dialogs that the browsers put up tell you that they are going to do a postback instead of just get'ting the page. And you can see in your code that the warnings are accurate - any handlers that were invoked on the original postback will get invoked a second time. This is one of the main problems with postbacks - they essentially break the refresh key. If you just want to load the page, you have to mouse up to the address bar and hit enter. This will load the page with IsPostback false.

Upvotes: 1

Oded
Oded

Reputation: 499012

Most browser will do a post of the same data if you refresh after posting.

This is the meaning of the dialogues you have seen (they are asking if you want to re-post - click yes/OK means a re-post).

In order to avoid the re-post, simply go to the address bar and press enter. That will cause a new request to be issues rather than a re-post.

Upvotes: 2

Related Questions