How to detect page refresh in .net How to detect page refresh in .net asp.net asp.net

How to detect page refresh in .net


using the viewstate worked a lot better for me as detailed here. Basically:

bool IsPageRefresh = false;//this section of code checks if the page postback is due to genuine submit by user or by pressing "refresh"if (!IsPostBack)     {    ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();    Session["SessionId"] = ViewState["ViewStateId"].ToString();}else{    if (ViewState["ViewStateId"].ToString() != Session["SessionId"].ToString())    {        IsPageRefresh = true;    }    Session["SessionId"] = System.Guid.NewGuid().ToString();    ViewState["ViewStateId"] = Session["SessionId"].ToString();}   


This article could be of help to youhttp://www.codeproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET

you are adding a Guid to your view state to uniquely identify each page. This mechanism works fine when you are in the Page class itself. If you need to identify requests before you reach the page handler, you need to use a different mechanism (since view state is not yet restored).

The Page.LoadComplete event is a reasonable place to check if a Guid is associated with the page, and if not, create one.

check thishttp://shawpnendu.blogspot.in/2009/12/how-to-detect-page-refresh-using-aspnet.html


Simple Solution

Thought I'd post this simple 3 line solution in case it helps someone. On post the session and viewstate IsPageRefresh values will be equal, but they become out of sync on a page refresh. And that triggers a redirect which resets the page. You'll need to modify the redirect slightly if you want to keep query string parameters.

    protected void Page_Load(object sender, EventArgs e)    {        var id = "IsPageRefresh";        if (IsPostBack && (Guid)ViewState[id] != (Guid)Session[id]) Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);        Session[id] = ViewState[id] = Guid.NewGuid();        // do something     }