The Problem

There are situations where we would like to detect if the postback is from a form interaction (i.e. submit or button clicks) or if it is by hitting the browser F5 refresh button.

Many of you will jump saying how about checking Ispostback value. IsPostback will always have the value which was set previously. So for instance, if the page was posted back before refresh, then the value will be true and if the page is not posted back before refresh, then the value will be false.

This article will first explain the fundamentals of how to solve the above problem and later this article will go in depth of how the source code looks like.

Step 3: In HttpHandler or HttpModule, the new GUID value is checked with the old GUID value. If the values are not equal, then it means this was not called from a submit click and it’s a refresh event. Accordingly the HttpContext session value is set.

Step 4: In the page load, we can then check if this was a refresh or postback using the session variables.

3 Important Parts of the Code

There are 3 important parts of the code to be understood:

JavaScript which generates the unique GUID.

HtppHandler which checks if the old value is equal to the new value.

ASP.NET page which finally checks if it’s a refresh or postback and handles logic accordingly.

JavaScript Code

So first let's start with the JavaScript code. The genereateRandomSequence function generates a unique GUID by using math functions like random and floor.

The onPostBack function calls generateRandomSequence function to generate GUID and attach the same to a hidden field with name hdnGuid.

This hidden field is generated on the fly in the HttpHandler module which will be explained shortly. Below is the code snippet for the same:

The next step is to create a simple HttpModule class which overrides the page_Init event and the page_Load event. In the page_Init event, we have created a simple hidden field by name hdnGuid which is attached to the page on the first hit itself.

In the page_Load event, we check if the hidden field value is the same as the old value. In case the value is not the same, that means it's a ‘postback’ and if the value is the same, then it's ‘refresh’. As per the situation, we set the httpContent.Items[“Refresh”] value.

If you see the original article , this article is different and explains the same thing in a simplified manner. The original article has a full open source but no code explanation which makes it quiet uncomfortable to use in projects.

danidanidani wrote:

adds no value

I do not know how many times i have felt the need to check if the page is post back or refresh.

This only works with JavaScript enabled - pretty obvious but could cause issues, particularly with the popularity of the NoScript addon for FF and similar functionality in other browsers. If JavaScript is disabled then the page is also flagged as being a refresh. I assume that the purpose of determining whether the request is a postback or a page refresh is to prevent duplicate actions being performed - which would cause it to be impossible to perform the action without Javascript.

Altering the Myhandler _page_Init so that the hdnGuid field is not initially set to an empty string will enable detection that Javascript is not enabled meaning that it would fall back to a postback.