In ASP.NET 2.0, you can setup a default button that the ENTER key submits by doing this:
<form DefaultButton="Button1" >
You can also add code to the form's onsubmit statement that will be executed when the page is submitted.
Problem:
If your onsubmit statement code attempts to prevent the page from being submitted, the DefaultButton feature will be turned off for the next time the user hits ENTER.
The steps are as follows:
1. Setup a web form with two buttons, each with a server-side Click event handler and add a TextBox. Set <form DefaultButton> to point to the second button on the page. (The first button is normally the default.)
2. Assign some code to RegisterOnSubmitStatement. This example uses a confirm messagebox:
Page.ClientScripts.RegisterOnSubmitStatement("confirm", "if (!confirm('Are you sure?')) return false;")
The use of return false in the script stops the page from submitting.
3. Run the page.
4. Put the focus into a textbox and hit ENTER.
5. It will prompt "Are you sure?". Click Cancel. At this time, the code Microsoft wrote for handling the default button will have run once.
6. Hit ENTER again. This time click OK to the prompt. The page will post back, but will always run the Click event handler for first button on the page.
Solution:
Microsoft's code sets up a global variable __defaultFired which is false before their code runs and true after it runs. Once true, their default button code will not handle the ENTER key again until the page is reloaded. The fix is to set "__defaultFired=false;"
in your onsubmit statement if you return false:
Page.ClientScripts.RegisterOnSubmitStatement("confirm", "if (!confirm('Are you sure?')) {__defaultFired=false; return false;}")
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
Star
7962 Points
5325 Posts
Using Form.DefaultButton when RegisterOnSubmitStatement
Jan 23, 2006 01:14 PM|PLBlum|LINK
ASP.NET 2.0 only.
In ASP.NET 2.0, you can setup a default button that the ENTER key submits by doing this:
<form DefaultButton="Button1" >
You can also add code to the form's onsubmit statement that will be executed when the page is submitted.
Problem:
If your onsubmit statement code attempts to prevent the page from being submitted, the DefaultButton feature will be turned off for the next time the user hits ENTER.
The steps are as follows:
1. Setup a web form with two buttons, each with a server-side Click event handler and add a TextBox. Set <form DefaultButton> to point to the second button on the page. (The first button is normally the default.)
2. Assign some code to RegisterOnSubmitStatement. This example uses a confirm messagebox:
Page.ClientScripts.RegisterOnSubmitStatement("confirm", "if (!confirm('Are you sure?')) return false;")
The use of return false in the script stops the page from submitting.
3. Run the page.
4. Put the focus into a textbox and hit ENTER.
5. It will prompt "Are you sure?". Click Cancel. At this time, the code Microsoft wrote for handling the default button will have run once.
6. Hit ENTER again. This time click OK to the prompt. The page will post back, but will always run the Click event handler for first button on the page.
Solution:
Microsoft's code sets up a global variable __defaultFired which is false before their code runs and true after it runs. Once true, their default button code will not handle the ENTER key again until the page is reloaded. The fix is to set "__defaultFired=false;" in your onsubmit statement if you return false:
Page.ClientScripts.RegisterOnSubmitStatement("confirm", "if (!confirm('Are you sure?')) {__defaultFired=false; return false;}")
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com