Home Contact

Jeroen Bourdeaud'hui

web application developer

News

Twitter












Archives

Post Categories

Image Galleries

.NET url

Friends/Other developers

Syndication:

How to prevent the user to post page twice, by using refresh

When a user post's data to the server, the page will be reloaded. When the user hits the F5/refresh button, a message appears, which tells us, that we´ve allready sended the data to the server. And asks us if we want to sent it again.
If the user clicks "yes", we have to catch that, we don´t want this in our application


- check that the user can only post something every 2 minutes.
- save the posted data in viewstate and check if something changed
- ...


Or we can check if the user hit the refresh button. I haven't found a way to check this in Javascript, which will work in all browser.

Or our solution is to check it in code. Add the following code, for example in a basepage.

    

     Private _refreshState As Boolean
    Private _isRefresh As Boolean
    Private _terminateRaisePostback As Boolean

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack AndAlso _isRefresh Then
            Response.Redirect(HttpContext.Current.Request.Url.ToString(), False)
            HttpContext.Current.ApplicationInstance.CompleteRequest()
            _terminateRaisePostback = True
        End If
    End Sub

    Protected Overrides Sub LoadViewState(ByVal savedState As Object)
        Dim AllStates As Object() = CType(savedState, Object())
        MyBase.LoadViewState(AllStates(0))
        _refreshState = Boolean.Parse(CStr(AllStates(1)))
        _isRefresh = _refreshState.Equals(CBool(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

    Protected Overrides Sub RaisePostBackEvent(ByVal sourceControl As System.Web.UI.IPostBackEventHandler, ByVal eventArgument As String)
        If Not _terminateRaisePostback Then
            MyBase.RaisePostBackEvent(sourceControl, eventArgument)
        End If
    End Sub

Thursday, September 29, 2011 9:51 PM

Feedback

No comments posted yet.


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: