Detect browser refresh

后端 未结 2 1084
感情败类
感情败类 2021-01-13 04:13

How can I find out if the user pressed F5 to refresh my page (Something like how SO implemented. If you refresh your page, the question counter is not increased). I have tes

相关标签:
2条回答
  • 2021-01-13 04:42

    I run into this problem and use the following code. It works well for me.

    bool isPageRefreshed = false;
    
    protected void Page_Load(object sender, EventArgs args)
    {
        if (!IsPostBack)
        {
            ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();
            Session["SessionId"] = ViewState["ViewStateId"].ToString();
        }
        else
        {
            if (ViewState["ViewStateId"].ToString() != Session["SessionId"].ToString())
            {
                isPageRefreshed = true;
            }
    
            Session["SessionId"] = System.Guid.NewGuid().ToString();
            ViewState["ViewStateId"] = Session["SessionId"].ToString();
        } 
    }
    
    0 讨论(0)
  • 2021-01-13 04:46

    The only sure solution is to make a redirect to the same page , and here is a similar question: Post-Redirect-Get with ASP.NET

    But there are also some other tricks, by adding some ticket on the page and see if this is the same or have change, see the full example and code at:

    http://www.codeproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET

    and one more:

    http://dotnetslackers.com/community/blogs/simoneb/archive/2007/01/06/Using-an-HttpModule-to-detect-page-refresh.aspx

    0 讨论(0)
提交回复
热议问题