Using asp.net, server-side events will be automatically triggered when the page is refreshed. Take a simple example, such as: a registration page, after we fill in the registration information, if we press F5 to refresh, the Button event will be automatically triggered, which will cause the trouble of registering again.
My previous solution: query with keywords, and if there are similarities, it will prompt that this user already exists.
But this solution cannot be used without recognition, and he will resubmit. In view of this, I looked for a new solution. Fortunately, with the advice of a friend, I came up with such a solution. There must be a better solution, and I will not hesitate to give you advice.
Solution: When refreshing, capture KeyPress and let it trigger other useless events.
1. JavaScript to capture the F5 event
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. Place a Button on the aspx page
<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 "save the car to keep the handsome" solution can solve the small problem of automatically triggering events for refresh. If anyone has a better solution, please let me know. I would be very grateful!