Click here to Skip to main content
14,768,089 members
Articles » Web Development » ASP.NET » General
Posted 25 Mar 2010

Tagged as


96 bookmarked

Detecting Refresh or Postback in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.34/5 (28 votes)
25 Mar 2010CPOL
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.

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.

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.

The Fundamental

Image 1

Step 1: We have created a JavaScript which will generate unique fresh GUID in submit.

Step 2 (User presses submit click): If user presses submit button, it will call the necessary JavaScript function to create the new fresh GUID again.

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.

Image 2

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:

function onPostBack()
var y=generateRandomSequence();
var hdnGuid=document.getElementById("hdnGuid"); 

function generateRandomSequence() 
var g = ""; 
for(var i = 0; i < 32; i++) 
g += Math.floor(Math.random() * 0xF).toString(0xF) 
return g; 

The above JavaScript function is referred in a JS file and called on the button submit click as shown in the below HTML code snippet:

<title>Untitled Page</title>
<script type="text/javascript" language="javascript" src="Client-Side_Validn.js"></script>
<form id="form1" runat="server" onsubmit="onPostBack()">
<asp:Button ID="Button1" runat="server" Text="Button" />

HttpModule Code

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
return 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";
if (!_page.IsPostBack)
hdnGuid.Value = Guid.NewGuid().ToString();

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.

void _page_Load(object sender, EventArgs e)
HiddenField h1 = (HiddenField)(_page.Form.FindControl("hdnGuid"));
GuidClass currentGuid =new GuidClass();
currentGuid.Guid= h1.Value;
System.Web.HttpContext _httpContext = System.Web.HttpContext.Current;

if (temp.Contains<string>(currentGuid.Guid))

We also need to ensure that the handler is registered in the httpModules tag.

<add name="Myhandler" type="Myhandler"/>

ASP.NET Page Code

The final part is to detect in the ASP.NET page whether it’s a refresh or postback. The below code demonstrates how HttpContext session can be referred to check the same and act accordingly.

if ((bool)HttpContext.Current.Items["IsRefresh"])


  • 25th March, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Authors

Archak Sainanee
Software Developer (Senior) Lionbridge
India India
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...

Comments and Discussions

QuestionHttpContext.Current.Items problem when I click the button Pin
akira326-Dec-17 21:37
Memberakira326-Dec-17 21:37 
AnswerRe: HttpContext.Current.Items problem when I click the button Pin
Member 977860827-Jun-18 20:43
MemberMember 977860827-Jun-18 20:43 
QuestionQuestion Pin
Sujit Keng23-May-14 4:52
MemberSujit Keng23-May-14 4:52 
QuestionDoesn't work with ScriptManager inplace Pin
phamdacloc27-Oct-13 19:56
Memberphamdacloc27-Oct-13 19:56 
Questionhow do i do it for a page having more than one button? Pin
tamal8867220-Aug-13 0:46
Membertamal8867220-Aug-13 0:46 
AnswerRe: how do i do it for a page having more than one button? Pin
Archak Sainanee20-Aug-13 5:35
MemberArchak Sainanee20-Aug-13 5:35 
QuestionNot working for me. Pin
Ajay Vaddoria7-Mar-13 6:33
MemberAjay Vaddoria7-Mar-13 6:33 
QuestionHow to add For MasterPage Pin
karuguvel24-Nov-12 1:22
Memberkaruguvel24-Nov-12 1:22 
AnswerRe: How to add For MasterPage Pin
Member 125202523-Dec-12 7:58
MemberMember 125202523-Dec-12 7:58 
QuestionCheck Page Refresh Pin
Member 812804011-Oct-12 8:05
MemberMember 812804011-Oct-12 8:05 
GeneralMy vote of 3 Pin
HaBiX18-Sep-12 20:52
MemberHaBiX18-Sep-12 20:52 
QuestionCursor Control after Postback and F5 Refresh Pin
sarndt16-Mar-12 7:26
Membersarndt16-Mar-12 7:26 
AnswerPossible alternative Pin
Ankur\m/19-Nov-10 2:26
professionalAnkur\m/19-Nov-10 2:26 
GeneralRe: Possible alternative Pin
cjc13327-Apr-11 14:56
Membercjc13327-Apr-11 14:56 
GeneralRe: Possible alternative Pin
Ankur\m/27-Apr-11 19:14
professionalAnkur\m/27-Apr-11 19:14 
GeneralRe: Possible alternative Pin
Najm us Sahar Imtiaz16-Nov-18 1:30
MemberNajm us Sahar Imtiaz16-Nov-18 1:30 
GeneralRe: Possible alternative Pin
Ankur\m/11-Jan-19 1:22
professionalAnkur\m/11-Jan-19 1:22 
GeneralOverload AppServer Pin
Rodrigo S. Teixeira14-May-10 12:25
MemberRodrigo S. Teixeira14-May-10 12:25 
GeneralAn eloquent solution through JS Pin
ShaunnyBoy29-Mar-10 23:01
MemberShaunnyBoy29-Mar-10 23:01 
QuestionWhat if javascript fail to run ? Pin
CoolVini27-Mar-10 7:44
MemberCoolVini27-Mar-10 7:44 
AnswerRe: What if javascript fail to run ? Pin
Archak Sainanee28-Mar-10 0:43
MemberArchak Sainanee28-Mar-10 0:43 
GeneralHi Companion Pin
Anil Srivastava25-Mar-10 20:27
MemberAnil Srivastava25-Mar-10 20:27 
GeneralThe Pictures(Diagrams) in this article Pin
thatraja25-Mar-10 20:14
professionalthatraja25-Mar-10 20:14 
GeneralRe: The Pictures(Diagrams) in this article Pin
User 151565629-Mar-10 23:08
MemberUser 151565629-Mar-10 23:08 
GeneralRe: The Pictures(Diagrams) in this article Pin
Shivprasad koirala30-Mar-10 1:57
MemberShivprasad koirala30-Mar-10 1:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.