Click here to Skip to main content
Click here to Skip to main content

Detecting Page Refresh

, 28 Apr 2005
Rate this:
Please Sign up or sign in to vote.
A common problem that Web application developers encounter is how to stop the user from refreshing the page. So What to do? Although we can’t stop the user from refreshing the page but we can determine if this event has already occurred and then take appropriate action.

Introduction

A common problem that Web Application developers encounter is how to stop the user from refreshing the page. The problem arises, if the previous request to the server was a PostBack, which, for example, inserts the WebForm’s data into a database. This will result in the addition of duplicate rows in the database. But we have a constraint that we can’t stop the user by refreshing the page. So, what to do? Although we can’t stop the user from refreshing the page, but we can determine if this event has already occurred and then take appropriate action.

This article is the result of inspiration from the article “Build Your ASP.NET Pages on a Richer Bedrock” by Dino Esposito in which the author has outlined a mechanism to detect page refreshes. But the problem with this method is that it is cumbersome and complicated, although the fundamental idea is sound and forms the basis of this solution. Dino’s mechanism uses a counter stored on the page and a session variable to store the previous request’s counter on the server, if the two match then we have a page refresh.

Strategy

My strategy will make use of the ViewState feature. As we are using ViewState, it would seem logical to perform the operation in the LoadViewState and SaveViewState methods. Using these two methods, instead of the OnLoad method, has more benefits in that it eliminates the potential problems of sub-classes implementing Page_Load. Let’s have a look at these methods first:

LoadViewState

The LoadViewState method loads the previously saved ViewState. The savedState object contains the saved view state values for the item.

SaveViewState

Saves any server control view-state changes that has occurred since the time the page was posted back to the server.

Returns the server control's current view state. If there is no view state associated with the control, this method returns a null reference (Nothing in Visual Basic).

How the process works

The LoadViewState method, which is part of the page’s initialization phase, is only invoked during PostBack and therefore the SaveViewState is the only method, of the two ViewState related methods, that is called when the page is first requested.

Note: _refreshState (which on initial page request is defaulted to False and on subsequent PostBack requests, is the value of ViewState) is assigned to the Session["__ISREFRESH"] item and the negated _refreshState is saved to the new ViewState.

Protected Overrides Function SaveViewState() As Object
 Session("__ISREFRESH") = _refreshState
 Dim AllStates() As Object = New Object(2) {}
 AllStates(0) = MyBase.SaveViewState
 AllStates(1) = Not (_refreshState)
Return AllStates
End Function

Once a PostBack event takes place the LoadViewState method is called:

Protected Overrides Sub LoadViewState(ByVal savedState As Object)
 Dim AllStates As Object() = savedState
 MyBase.LoadViewState(AllStates(0))
 _refreshState = Boolean.Parse(AllStates(1))
 _isRefresh = _refreshState = Session("__ISREFRESH")
End Sub

Note: The _refreshState is retrieved from the ViewState and compared with the value in the Session["__ISREFRESH"] item. The result is stored in _isRefresh, which is used by the IsRefresh property.

The listing below shows the entire class definition:

Public Class test1
   Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "
   'This call is required by the Web Form Designer.
   <System.Diagnostics.DebuggerStepThrough()> 
   Private Sub InitializeComponent()
   End Sub

   Protected WithEvents Button1 As System.Web.UI.WebControls.Button
   Protected WithEvents label1 As System.Web.UI.WebControls.Label

   'NOTE: The following placeholder declaration is 
   'required by the Web Form Designer.

   'Do not delete or move it.

   Private designerPlaceholderDeclaration As System.Object

   Private Sub Page_Init(ByVal sender As System.Object, 
             ByVal e As System.EventArgs) Handles MyBase.Init
   'CODEGEN: This method call is required by the Web Form Designer
   'Do not modify it using the code editor.

   InitializeComponent()

   End Sub

#End Region

   Private _refreshState As Boolean
   Private _isRefresh As Boolean

   Private Sub Page_Load(ByVal sender As System.Object, 
            ByVal e As System.EventArgs) Handles MyBase.Load
     'Put user code to initialize the page here
     Me.label1.Text = _isRefresh.ToString
   End Sub

   Protected Overrides Sub LoadViewState(ByVal savedState As Object)
     Dim AllStates As Object() = savedState
     MyBase.LoadViewState(AllStates(0))
     _refreshState = Boolean.Parse(AllStates(1))
     _isRefresh = _refreshState = Session("__ISREFRESH")
   End Sub

   Protected Overrides Function SaveViewState() As Object
     Session("__ISREFRESH") = _refreshState
     Dim AllStates() As Object = New Object(2) {}
     AllStates(0) = MyBase.SaveViewState
     AllStates(1) = Not (_refreshState)
     Return AllStates
   End Function
End Class

When you click the control which fires PostBack event, LoadViewState event is executed. This function loads the previously saved ViewState and here we retrieve the refresh state to find out whether the page is requested to be refreshed. The crucial part is checking whether the value of _refreshstate and the one stored in session is the same, if yes it means the user has hit the Refresh Button.

The SaveViewState function saves the current _refreshState value in session so that we can check this later on, to find out whether the Refresh button is hit.

Conclusion

In this article I have tried to demonstrate a simplified method of detecting a page refresh event.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Altaf Al-Amin

United Arab Emirates United Arab Emirates
Altaf Al-Amin Najvani
Project Manager
Commercial Bank Of Dubai
 

Qualifications:
BS - Computer Science
Masters In Project Management
Masters In Business Administration
 
Certifications:
CISA - Certified Information Systems Auditor
ITIL (Foundation)
Microsoft Certified Technology Specialist (SQL Server 2005)
Microsoft Certified Technology Specialist (Web Applications)
Microsoft Certified Application Developer (.NET)
Microsoft Certified Solution Developer (.NET)

Comments and Discussions

 
GeneralDoes not work Pinmembernssidhu20-Nov-07 6:42 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 29 Apr 2005
Article Copyright 2005 by Altaf Al-Amin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid