Detect browser refresh

风格不统一 提交于 2019-12-01 05:36:09
Aristos

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

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();
    } 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!