Solution to prevent auto-triggered events from refreshing in ASP. Net

Source: Internet
Author: User

When you use asp.net to refresh the page, events on the server are automatically triggered. For example, after entering the registration information on a registration page, if you press F5 to refresh it, it will be automatically triggered to the Button event, in this way, the trouble of registering again is caused.

My previous solution: Query with keywords. If they are the same, the system prompts that the user already exists.

However, this solution cannot be used without table recognition. It will be submitted again. In view of this, I have searched for a new solution. Fortunately, with my friend's suggestion, I have provided such a solution. I am sure I have a better solution.

Solution: capture KeyPress during refresh to trigger other useless events.

1. JavaScript for capturing F5 events
Required parameter Doc ument. 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 button#click (object sender, System. EventArgs e)
{
Response. Write ("You have pressed the key F5 ");
}

This "Car Care coach" solution can solve the small problem of refreshing the automatically triggered event solution. If anyone has a better solution, I 'd like to say it to you. I am very grateful!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.