page refresh after postback

No Gravatar

The classic problem of a user refreshing his browser after a postback has been bugging me for about half of a year. I just haven’t had the time to fix it.
As you all know already, the problem occurs when the user has posted data which is inserted into a database, for instance. After the post, if the user refreshes the browser a dialog opens asking whether he wants to repost. If he agrees then the post is sent a second time and we have inserted the same data twice in the database.
A number of articles on the internet have dealt with this issue. I don’t want to post them here, but trust me that I tried them all and none of them worked. Either they weren’t adaptable to a master page, or to ajax, or some other problem.
Finally I did my own code. I tested it and it seems to work in all cases. It can be implemented in the master page which means it can cover all your admin pages with one shot.
Here is the code:
<asp:HiddenField ID="hdnRefresh" runat="server" />
First place a hiddenfield in the master page. I am using ajax so I put it in an updatepanel where the updatemode is set to always (important).
Next add this code to the master codebehind:
If IsPostBack Then
End If
Private Sub CheckRefresh()
If Session("refresh") Is Nothing And String.IsNullOrEmpty(hdnRefresh.Value) Then
hdnRefresh.Value = "0"
Session("refresh") = hdnRefresh.Value
ElseIf CInt(Session("refresh")) > CInt(hdnRefresh.Value) And CInt(hdnRefresh.Value) >= 0 Then
Session("refresh") = Nothing
End If
Catch ex As Exception
End Try
End Sub

Here is the explanation:
The refresh problem is that when a user refreshes his page after a post, the browser sends the last copy of the post back to the server.
In other words, let’s say a user has posted data in stage 1. In stage 2, the server receives the post and return a response back to the browser. That response may have changes to viewstate in it. The new page is now viewed by the user. If he/she presses refresh (f5) then the browser send a copy of the previous post back to the server (without the new changes made in the last server response.).
Herein lies the solution. We have a hiddenfield which is tied to the viewstate, as well as a session variable which is not connected to viewstate.
After a post, the hiddenfield is updated together with the session variable. If the user refreshes, then the previous value of the hiddenfield is sent to the server, whereas the current session value is retained. Thus, in a case where the server checks and finds that the hiddenfield value and the session value are not equal, we have a refresh and the user is flown off to the error page in order to force him/her to reenter the page anew.
You might notice in the code a few oddities. I don’t want to through all of them, but know this: I was experiencing the double postback syndrome for ajax updatepanels. This was ruining the code, because it was changing the session variable but not the hiddenfield. The double postback is a known issue. I couldn’t get to the bottom of it, even after checking to make sure that all of my images and external js files had legitimate urls. So, I had to change our code a little.
First, no check is made where we are not in a postback. Also, the hiddenfield has to be at least 0 (cause in the double postback the value of the hiddenfield is reset to nothing.)
In any event this should work. If it doesn’t work for you, please let me know with a comment.

2 Responses to “ page refresh after postback”

  1. 1 Hiral ShahNo Gravataron 19 Apr 2011 at 10:33 link to this comment


    This will not work after page is postback due to dropdown selectedindexchanged.


  2. 2 Adam SolomonNo Gravataron 15 Jul 2011 at 10:16 link to this comment

    Hiral, can you specify in more detail the problem you are having?


Trackback URI | Comments RSS

Leave a Reply

FireStats icon ‏מריץ FireStats‏