How to find whether the user clicks browser back button or Refresh button

前端 未结 8 1337
感动是毒
感动是毒 2021-02-07 12:59

I need to find whether the user clicking the browser back button or Refresh button.

I need to redirect the page to Error page when he clicks the back or refresh button.

8条回答
  •  难免孤独
    2021-02-07 13:52

    Implement a PageToken using a guid/or timestamp stored in session and compare the value to a hidden field on the form. I did this via a PageToken Class. If the value from the hidden field and the session variable don't match then you're out of synch and you handle that. The trick is to map all events on your page.

    public void GeneratePageToken()
    {
        SessionVariables currSession = new SessionVariables();
        hfMasterPageToken.Value = System.DateTime.Now.ToString();
        currSession.PageToken = hfMasterPageToken.Value;
    }
    
    public string GetLastPageToken
    {
        get
        {
            SessionVariables currSession = new SessionVariables();
            return currSession.PageToken;
        }
    }
    
    public bool TokensMatch
    {
        get
        {
            SessionVariables currSession = new SessionVariables();
            return (currSession.PageToken != null
                && currSession.PageToken == hfMasterPageToken.Value);
        }
    }
    

    In your Event method before your regular code:

    if (this.TokensMatch == false)
    {
        //Reload the data.
        //Generates a NewPageToken (this.GeneratePageToken();)
        ReloadDataMethod();
        this.MessageToUser =
         "Data reloaded.  Click Edit or Insert button to change.";
        this.MessageType = MessageToUserType.Success;
        this.DisplayMessageToUser = true;
        return;
    }
    

提交回复
热议问题