Posts
33
Comments
186
Trackbacks
0
Detecting browser 'Refresh' from Code behind in C#

Browser 'Refresh' is always a cause of concern for the developers. It becomes even more worse when the page interacts with the database. As each refresh, if not handled, would lead to the database action repeated.

This could lead to inconsistency in database or even break the application.

One way could be to detect 'refresh' using javascript and disable F5 or right click..but there are so many other ways end-user can initiate 'Refresh' action for e.g. by using CTRL+R on the keyboard...

The best way to stop 'Refresh' calling your program again is to detect it at the server side and handle it..

The following code snippet detects 'Refresh' in page_load function

bool IsPageRefresh = false;

//this section of code checks if the page postback is due to genuine submit by user or by pressing "refresh"

if (!IsPostBack)

    
{

ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();

Session["SessionId"] = ViewState["ViewStateId"].ToString();

}

else
 
{

if (ViewState["ViewStateId"].ToString() != Session["SessionId"].ToString())

{

IsPageRefresh = true;

}

Session["SessionId"] = System.Guid.NewGuid().ToString();

ViewState["ViewStateId"] = Session["SessionId"].ToString();

}     
 


You can then use the 'IsPageRefresh' boolean flag in the code-behind to determine if it's a postback due to genuine User submit action or by browser 'Refresh'.

Hope this helps!

posted on Wednesday, June 8, 2011 4:34 PM Print
Comments
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
Dave R.
9/29/2011 1:32 PM
This is exactly what I needed. Thank you. Much appreciated.
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
Asif Iqbal
7/24/2012 10:21 AM
I tested your solution but its not working if user has pressed F5 or simply refreshed page.

Reason: On page load you put a check "if (!IsPostBack)", value of IsPostBack remains false on page refresh.
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
Naina
5/1/2013 8:41 AM
Thanx for helping. good blog
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
anupam roy
7/4/2013 7:55 AM
completely agree with Asif Iqbal ... the logic was right to compare old and new id
but u did reinitialized ur old id even on refresh
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
Ritasree
8/24/2013 11:03 AM
This is excellent. Works for me..This is what i exactly need.Thanks for help..
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
Anil Jagtap
9/16/2013 1:08 PM
This is excellent, but i have one problem my page has 10 panel and one common next button . when goes to step no 5 and then press refresh button then step no 4 is display but i want on same step is no 5

Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
nisar Ahmad
5/17/2014 1:58 PM
you describe in very short code it is really usable for me
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
Mustafa B.
5/21/2014 2:35 PM
thanks a lot, Vipin! I think it's the best solution to get difference between postback and refresh of a page at server side! Awesome!
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
C#_Dre
6/27/2014 3:21 PM
Excellent and to the point.
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
Mohan
7/22/2015 8:33 AM
Excellent and Easy Understandable code...Awesome
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
Aakash
9/25/2015 10:56 PM
Not working as expected, I just did a simple page refresh and code assigns the a fresh value in viewstate and session.
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
piyush
6/9/2016 11:52 AM
thanxs man....
Gravatar
# re: Detecting browser 'Refresh' from Code behind in C#
Dov Miller
10/3/2017 1:54 PM
I had Asif Iqbal's and anupam roy's problem. I think it's because I'm trying to detect a refresh after initial page load and not after Postback therefore the isPostback is always false! Do you have an idea of how to change the code inorder to work after refresh after initial loading of the page?

Post Comment

Title *
Name *
Email
Comment *  
Verification