Prevent page refresh when pressing F5

I have a form with server-side control, for example button..and also wrote an event on this .. Now, the runtime after clicking the button I updated the page by pressing F5.Page_load does fine, but the button1_click () event also fires ... So how can i stop this event in this scenario. Please offer me

+4
source share
9 answers

Short answer: impossible

The long-term answer: No website can block browser functionality, as this will be a serious security issue for the browser.

+11
source

Welcome to web development, there is no way to stop page refresh and resubmission, this is completely browser behavior.

However, there is work; One simple thing you can do is, after processing a click on a button, you can redirect the browser back to the page so that the update does not include repeated button clicks. Obviously, this may require a different state management system (rather than a presentation state), perhaps you can save your state in a session.

+6
source

You can use:

window.onbeforeunload = function () { return false; } 

A prompt appears asking the user if they want to move around and may not be desirable. (In addition, I do not know the degree of support)

+2
source

Poster uses ASP.NET WebForms. In MVC, using the Post-Redirect-Get pattern, this will not be a problem.

But this was decided for Webforms since 2004 (and even before that - I wrote a simple ticket application in 2001) with this article: http://msdn.microsoft.com/en-us/library/ms379557.aspx

Basically, the author creates a unique ticket associated with each request, and deletes the validity of the ticket on the first postback. Any random (or malicious) subsequent callbacks are ignored. It is wrapped on the base page, so it is easy to add to any Webforms solution.

+2
source
  <script type="text/javascript" language="javascript"> function checkKeyCode(evt)// for F5 disable { var evt = (evt) ? evt : ((event) ? event : null); var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); if (event.keyCode == 116)//disable F5 { evt.keyCode = 0; return false } if (event.keyCode == 123) { evt.keyCode = 0; return false } if (event.keyCode == 93) { evt.keyCode = 0; return false } if (event.altKey == true && event.keyCode == 115) //disable alt-F4 { evt.keyCode = 0; return false } //alert(event.keyCode); } document.onkeydown = checkKeyCode; </script> 

Send this script on the main page or where you want to lock the F5 button.

+2
source

This is actually not an update to the page you want to block, its POST page.

As already mentioned, this is not possible on the client side, because the update will reproduce what the browser did in the last action.

But you can lure it to the server, because the server can store some information to manage the request.

The easiest way is to put the RequestID view in a hidden field in Page Load , save it somewhere, like in Session . And control its value in the next POST to execute the request, then redefine it to avoid re- POST with the same RequestID.

Maybe the best way :)

Your β€œproblem” is similar to user double-click behavior: ASP.Net double-click problem

+1
source

Need to refresh a page with a click of a button? Could you rewind the button to make an ajax call to send data? If so, then you don’t need to worry about re-sending the page when you refresh.

+1
source

The only solution to the F5 update problem is Response.Redirect . Please see my post on this topic.

https://stackoverflow.com/questions/12596152/asp-net-f5-browser-refresh-duplicate-record/12596153#12596153

0
source

I have code that does not allow you to press F5 and / or Ctrl + R:

 <!DOCTYPE html> <html> <head> <title> Key code test! </title> <script type="text/javascript"> var x, ctrlKeyPressed = false; function keyDown(e){ x = e.charCode || e.keyCode; document.getElementById("key").innerHTML = x; if(x == 17) { ctrlKeyPressed = true; } if(ctrlKeyPressed && x == 82) { e.preventDefault(); } else if(x == 116) { e.preventDefault(); } } function keyUp(e) { x = e.charCode || e.keyCode; if(x == 17) { ctrlKeyPressed = false; } } window.onload = function () { document.onkeydown = function (e) { keyDown(e); }; document.onkeyup = function (e) { keyUp(e); }; } </script> </head> <body> Press a key! <br/> Key code: <div style="display:inline;" id="key">Nothing</div> </body> </html> 
0
source

All Articles