This tip extends on the "Add Y/N confirm on ASP:Button" (
http://www.hintsandtips.com/ShowTip/99/hat.aspx) posted by Rocky Moore on July 16, 2004.
The above tip is great for adding a client side confirmation to a button. However, if you use .NET Validators and configure them to run on the client (EnableClientScript=true), the code in the above link will bypass the client side validation. Here's why...
When the page loads, .NET appends javascript code to the onclick event to fire the client side validation. This appended code appears AFTER the confirmation javascript code.
In the above tip's code, the "return" keyword is used to retrieve a true or false value from the user. Regardless of the value, the return keyword discontinues execution. Since the return keyword is always encountered before the validation code, the script is aborted before the validation code can be ran.
Here is the line of code from the above tip...
TheButtonToGenerateTheConfirm.Attributes["onclick"] = "return confirm('Are you sure?')";
Here is a modified line of code that will allow the client side validation to run...
TheButtonToGenerateTheConfirm.Attributes["onclick"] = "if(!confirm('Are you sure?')){return false};";
**NOTE: Be sure to include the semi-colon after the last bracket...}; If you do not include the semi-colon, then you will receive a javascript error. This is because the client side validation code is appended to the confirmation code. You must use the semi-colon to separate the two pieces of code.