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 "
<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
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
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
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.