Hints And Tips research knowledge archive
Tip# 107
Sponsored Links
 
Subject: Add Y/N confirm on ASP:Button (IE Only)...with Validation
Updated: Aug-13-2004
Rating: 10.00   (1 Votes)
By: Ryan Bost - Member #: 41
Location: Statesville, NC
Category: Computers > Programming > .NET > ASP.NET
Rate This Tip (10=Great):  1  2  3  4  5  6  7  8  9  10
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.

[Submit DIGG for this tip]


-----------
Note: Use the tips posted on this site at your own risk. The tips are posted by the public and as such may or may not be valid.

Sponsored Links
     
Categories
hints and tips gold divider
Sponsor Sites To Visit
www.SaintsInLight.com
Thoughts on the Christian walk
along various Christian links.

CICK HERE
www.BringingInTheHarvest.com
Christian:  The harvest is ripe and
the day is short, we need to bring in
the harvest while there is time!

CLICK HERE
XML RSS News Feed For Recently Posted Hints And Tips  RSS Feed
You can now get updates to the Recent Tips section by using RSS. The address is:

HintsAndTips.com/Rss.aspx

* Posting Tips *
To post a Tip, Recommendation or Tips Wanted, simply browse to the category you feel is a best fit for your post (click on the Recommention or Tips Wanted tab if fitting) and then click on "New Post" option.
(C) Copyright 1998-2010 All Rights Reserved