Avoid Re-execution of code when user refresh the page

Sub Page_Load (sender As Object, e As EventArgs)
    If Not Page.IsPostBack
        Session("update") = Server.URLEncode(System.DateTime.Now.ToString())
    End If
End Sub

Sub Page_PreRender (sender As Object, e As EventArgs)
    ViewState("update") = Session("update")
End Sub

Sub Button1_Click(sender As Object, e As EventArgs)
    If Session("update").ToString() = ViewState("update").ToString() Then

        'INSERT CODE HERE
           
        Session("update") = Server.URLEncode(System.DateTime.Now.ToString())
    End If
End Sub

2 comments:

  1. hi Nimish,
    hope you are doing well.
    your code is running properly, but i m unable to get in how this is happening,please help me how the viewstate and session variable value got different when user press refresh button,please reply.
    i would be highly appreciate your response.
    plz plz plz
    you can mail me on sunilgupta333@gmail.com

    ReplyDelete
  2. sunil it is happenning due to the order of page events. plz study page life cycle
    http://msdn.microsoft.com/en-us/library/ms178472.aspx

    ReplyDelete