How to detect page refresh in .net

I have a Button_click event. During page refresh, the previous Postback event is Postback again. How to detect page refresh event to prevent Postback action?

I tried the code below to solve this problem. In fact, I am adding a visual website on a SharePoint page. Adding webpart is a writeback event, so postback is always false every time I add a webpage to a page and I get an error in the else loop because the reference to the object is null .

 if (!IsPostBack){ ViewState["postids"] = System.Guid.NewGuid().ToString(); Cache["postid"] = ViewState["postids"].ToString(); } else{ if (ViewState["postids"].ToString() != Cache["postid"].ToString()){ IsPageRefresh = true; } Cache["postid"] = System.Guid.NewGuid().ToString(); ViewState["postids"] = Cache["postid"].ToString(); } 

How to solve this problem?

+14
source share
5 answers

Using viewstate worked a lot better for me, as detailed here . Mostly:

 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(); } 
+10
source

This article can help you http://www.codeproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET

you add Guid to your view state to uniquely identify each page. This mechanism works great when you are in the Page class itself. If you need to define queries before you reach the page handler, you need to use a different mechanism (since the view state has not yet been restored).

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

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

+5
source

If you want to detect an HTTP GET update, not just a POST, here is a hacking job that works mostly in modern browsers.

JavaScript:

 window.onload = function () { // regex for finding "loaded" query string parameter var qsRegex = /^(\?|.+&)loaded=\d/ig; if (!qsRegex.test(location.search)) { var loc = window.location.href + (window.location.search.length ? '&' : '?') + 'loaded=1'; window.history.replaceState(null, document.title, loc); } }; 

WITH#:

 public bool IsPageRefresh { get { return !string.IsNullOrEmpty(Request.QueryString["loaded"]); } } 

When the page loads, it will change the QueryString loaded=1 parameter without reloading the page (again, this - window.history.replaceState - works only in post-archaic browsers ). Then, when the user refreshes the page, the server can check for the presence of the loaded parameter of the query string.

Caution: basically works

The case when this does not work is when the user presses the address bar and presses enter . That is, the server will generate a false positive, detecting an update, when there is a possibility, the user actually wanted to reload the page fresh.

Depending on your goals, this may be desirable, but as a user it would drive me crazy if I expected it to reset on the page.

I did not think too much about this, but it would be possible to write some kind of magic to distinguish the update from reset through the address bar using any / all:

  • SessionState (assuming SessionState enabled), and the value of the loaded QueryString parameter
  • window.onbeforeunload event listener
  • keyboard events (detect F5 and Ctrl + R to quickly change the URL back to remove the loaded QueryString parameter), although it would be false negative for the browser refresh button to be pressed)
  • biscuits

If someone comes up with a solution, I would like to hear it.

0
source

Another way to check page refresh. I wrote my own code without a Java script or any client side.

Not sure if this is the best way, but I feel good.

 protected void Page_Load(object sender, EventArgs e) { if ((Boolean)Session["CheckRefresh"] is true) { Session["CheckRefresh"] = null; Response.Write("Page was refreshed"); } else { } } protected void Page_PreInit(object sender, EventArgs e) { Session["CheckRefresh"] = Session["CheckRefresh"] is null ? false : true; } 
0
source

A simple solution

I thought that I would lay out this simple solution of 3 lines, if it helps anyone. Once published, the IsPageRefresh values โ€‹โ€‹of the session and viewstate will be equal, but they will not synchronize when the page is refreshed. And that causes a redirect that resets the page. You will need to change the redirection a bit if you want to save the 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 } 
0
source

All Articles