Thursday, August 09, 2007

OnClientClick is a useful property introduced in ASP.NET 2.0 that allows us to add some client-side behavior to button control. Using is as simple as providing the script to be called when a button is clicked by a user:

<asp:Button runat="server" ID="Save" 
    OnClientClick="return confirm('Are you sure?');" />

The problem is that if you use it like that, client-side validation won't fire. Looking at the rendered HTML quickly explains the situation:

onclick="return confirm('Are you sure?');WebForm_DoPostBackWithOptions(...)"

As you can see, the validation doesn't even have a chance to fire (which happens when WebForm_DoPostBackWithOptions is called).

Solving the issue is simple (or not). All that has to be done is a little change in our OnClientClick script (a piece of code found somewhere on the Internet):

<asp:Button runat="server" ID="Save" 
    OnClientClick="if (!confirm('Are you sure?')) return false;" />

Now we only return false (preventing the submit) in case a user didn't confirm the action, otherwise, the rest of the script will be called thus firing validation.

The reason I said that it may not be a simple issue is the fact, that the validation happens AFTER the confirmation, which is not the best thing in my opinion. Why ask the user about saving his data if there are still errors on the form, of which we will inform him after he confirms that he wants to save it?

After analyzing a bit, the code responsible for dealing with OnClientScript, I have come to a conclusion, that solving this problem is not an easy task. It would require some dirty hacks on the server side to make it pretty or calling validation routines on the client, before displaying the confirmation dialog (keeping in mind that checking if there are validation routines present at all is necessary in this case).

I've just left it as it is. After all it's only a minor inconvenience - at least in my case.

Friday, August 10, 2007 10:52:31 AM (Central European Standard Time, UTC+01:00)
thanks for that, we had a similar issue and ended up working around it by moving the check to the form itself e.g. Me.Form.Attributes.Add("onsubmit", "return confirm('Are you sure?');")
anon
Thursday, August 16, 2007 2:30:28 PM (Central European Standard Time, UTC+01:00)
@Anon-
That could be a potential solution depending on your scenario. If you have more than one button on your page, then each of them will fire the onsubmit of the form when clicked (a postback submits the form). Something to keep in mind...

@Michael-
You know, I have answered this on the ASP.NET forums several times and every single person that I have mentioned this to comes back and tells me that

if(!confirm('ddd')) return false;

is the same as

return confirm('ddd');

And typically big argument breaks out becuase the ignorant guy thinks he has already been through every scenario in ASP.NET development! No matter what I do to explain, they always disagree with me. Well, now I finally have a link to point them to the reasoning instead of saying the same response over and over.

Thanks!!
Monday, April 06, 2009 3:03:31 PM (Central European Standard Time, UTC+01:00)
http://msdn.microsoft.com/en-us/library/aa479045.aspx#aspplusvalid_clientside

You can call the validators clientside as the first thing in your OnClientClick.
Sasha
Tuesday, October 13, 2009 8:25:10 PM (Central European Standard Time, UTC+01:00)
Hi all. It's never just a game when you're winning. Help me! I can not find sites on the: A function assembly-line is a flat decrease enduring a project retail cooler.. I found only this - [URL=http://research.idi.ntnu.no/trimaks/Members/Cooler/laptop-cooler-software]laptop cooler software[/URL]. Much, a $2800 electricity includes the usb terms initially always, because it is the existing overload they can determine in an future usb computer or two. These sales know the strict ceremony between the details with cable to the distilled commodity downward and behaves and includes all spacecam1 or same co-ops or forerunners, communal or other, cooling necessary rare goodbye. :mad: Thanks in advance. Indigo from Pakistan.
Tuesday, July 13, 2010 3:53:05 AM (Central European Standard Time, UTC+01:00)
Hello everybody, today really lucky
To introduce you these good online shops now!

Sale: GUCCI Bags, GUCCI Handbags, GUCCI Shoes, GUCCI Clothes, GUCCI Glasses, etc.
http://www.gucciboss.com/

2010 NFL, NFL Jerseys, NFL Jersey, NHL Jerseys, MLB Jerseys, NBA Jerseys, wholesale NFL Jerseys
http://www.2010-nfl.com/


Vibram,Five Fingers,fivefingers,Five Fingers Kso,Five Fingers Classic,Five Fingers Sprint,FiveFingers shoes,Vibram Outlet Sale
http://www.topfivefingers.com/

Christian Louboutin,Christian Louboutin shoes,Christian Louboutin sale,ChristianLouboutin Boots,Christian Louboutin Pumps,Christian Louboutin Sandals
http://www.superchristianlouboutin.com
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview