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 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
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.
Please feel free to download my free 500 question and answer eBook which covers .NET , ASP.NET, SQL Server, WCF, WPF, WWF, SilverLight, Azure by clicking here.
The solution which this article proposes first came in an ASP.NET book written by Dino Esposito and it was taken further by Mr. SimoneB to make a small open source code which you can get here.
A brief explanation is also provided at this link.
The article will go step by step to explain the ideas proposed by Mr. Dino Esposito in a simplified manner.
Step 3: In
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:
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.
genereateRandomSequence function generates a unique GUID by using
math functions like
onPostBack function calls
generateRandomSequence function to generate GUID and attach the same to a hidden field with name
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:
var g = "";
for(var i = 0; i < 32; i++)
g += Math.floor(Math.random() * 0xF).toString(0xF)
<form id="form1" runat="server" onsubmit="onPostBack()">
<asp:Button ID="Button1" runat="server" Text="Button" />
The next important code is the
HttpModule code. As a first step, let’s create a simple GUID class which will help us store the GUID values as shown in the below figure:
public class GuidClass
private string guid;
public string Guid
guid = value;
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.
void _page_Init(object sender, EventArgs e)
HiddenField hdnGuid = new HiddenField();
hdnGuid.ID = "hdnGuid";
hdnGuid.Value = Guid.NewGuid().ToString();
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
void _page_Load(object sender, EventArgs e)
HiddenField h1 = (HiddenField)(_page.Form.FindControl("hdnGuid"));
GuidClass currentGuid =new GuidClass();
System.Web.HttpContext _httpContext = System.Web.HttpContext.Current;
We also need to ensure that the handler is registered in the
<add name="Myhandler" type="Myhandler"/>
ASP.NET Page Code
The final part is to detect in the ASP.NET page whether it’s a
postback. The below code demonstrates how
HttpContext session can be referred to check the same and act accordingly.
- 25th March, 2010: Initial post
I am an engineer at a midsized software firm.
My preferred language of choice is T-SQL.
And I am a big Pink Floyd fan...