|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionA 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. StrategyMy strategy will make use of the ViewState feature. As we are using ViewState, it would seem logical to perform the operation in the LoadViewStateThe SaveViewStateSaves 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 How the process worksThe Note: 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 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 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, The ConclusionIn this article I have tried to demonstrate a simplified method of detecting a page refresh event.
|
||||||||||||||||||||||