vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Friday, August 12, 2011

Detect page refresh or F5 press event in asp.net


hi..
In this article u know about to detect refresh button in asp.net...For some case we don’t pass refresh event to any code. By detecting f5 we can easily bypass the code..Its very simple just write the below code in your page ,when you press F5 button it detect its store Boolean value  refresh= true..By using refresh variable we can easily detect f5 wherever in that page....

Also  when upload file using upload control, After uploading when press F5 again its uploading the same file ...by avoiding such a event  You  can check refresh event by using this code..

For an example here i added one button in a page when a press F5 it detect refresh then button event has pass the value...

Just write the code wherever in the page...
Private refreshState As Boolean
Private Refresh As Boolean

 Protected Overrides Sub LoadViewState(ByVal savedState As Object)
        Dim AllStates As Object() = savedState
        MyBase.LoadViewState(AllStates(0))
        refreshState = Boolean.Parse(AllStates(1))
        Refresh = 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

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Refresh = True Then
            Response.Write("hi")
        End If
    End Sub

I hope this one helpful to all…..

0 comments:

Post a Comment