Geeks With Blogs
Jeroen Bourdeaud'hui web application developer

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
Posted on Thursday, September 29, 2011 9:51 PM asp.net , security , javascript , vb.net | Back to top


Comments on this post: How to prevent the user to post page twice, by using refresh

# re: How to prevent the user to post page twice, by using refresh
Requesting Gravatar...
Why your codes are white? I can't read it unless I select the whole code pieces.
Left by walkingp on Jan 17, 2014 3:08 AM

Your comment:
 (will show your gravatar)


Copyright © Jeroen Bourdeaud'hui | Powered by: GeeksWithBlogs.net