ASP.Net solution to prevent the refresh of auto triggered events

  • 2020-05-05 11:10:44
  • OfStack

With asp.net, server-side events are automatically triggered when the page is refreshed. Take a simple example: a registration page, after we fill in the registration information, if we press F5 to refresh, it will automatically trigger the Button event, which causes the trouble of registering again.

My previous solution: query with the keyword, and if there is the same, prompt the existing user.

However, this scheme cannot be used without a form, it will be resubmitted. In view of this, I looked for a new solution, very fortunately, in the point of my friend, burst into such a solution, there must be a better solution, do not hesitate to give advice.

Solution: when refreshing, capture KeyPress and let it trigger other useless events.

1, captures JavaScript

for F5 events

window.document.onkeydown = KeyStroke;
function KeyStroke()
{
  var key = event.keyCode;
  event.srcElement.releaseCapture();
  if(key == 116)
  {
  document.getElementById("Button1").click();
  event.keyCode=0;
  event.returnValue=false;
  }
}

2, aspx page place an Button

    < asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 344px; POSITION: absolute; TOP: 408px; WIDTH: 0px;" runat="server"
      Text="Button" > < /asp:Button >

3, Button event

private void Button1_Click(object sender, System.EventArgs e)
  {
              Response.Write( "You have pressed the key F5");
  }

This "car care" solution, can solve the refresh automatic trigger event solution this small problem, if anyone has a better solution, please let me know, I will be grateful!


Related articles: