OnClientClick with confirm doesn't work

M

Mauricio Pires

My GridView has a button with OnClientClick="if (!confirm('Delete?')) return
false;" which must deletes a record only if the user click on the OK button
of the confirmation window, but if he clicks on the Cancel button the record
is also deleted, that is, it is not working.

Could someone help me?
 
J

Jester98x

My GridView has a button with OnClientClick="if (!confirm('Delete?')) return
false;" which must deletes a record only if the user click on the OK button
of the confirmation window, but if he clicks on the Cancel button the record
is also deleted, that is, it is not working.

Could someone help me?

Hi Mauricio,

Had same problem myself some time ago.

Solution was to use OnClientClick="return confirm('Delete?');"

Hope this helps.
Steve
 
R

rstrahl

You need to also set the Submit behavior I think. If you leave it at default
a Submit button is generated which will always submit. If you use
UseSubmitBehavior == false the button gets generated as a button in which
case you can control submission manually:

<asp:Button runat="server" id="myButton" UseSubmitBehavior="False"
OnClientClient="if (!confirm("Delete?") return;
window.forms[0].submit();" />

+++ Rick ---
 
J

Jester98x

rstrahl,

You don't need to use the UseSubmitBehavior in this instance as the
OnClientClick="return confirm('Delete?');" will return false if the
user selects No on the confirmation box, which in turn blocks the
button click event from bubbling any further. i.e. stopping the form
submission. OnClientClick="return confirm('Delete?');" is basically
a shorthand version of your code.

Steve

You need to also set the Submit behavior I think. If you leave it at default
a Submit button is generated which will always submit. If you use
UseSubmitBehavior == false the button gets generated as a button in which
case you can control submission manually:

<asp:Button runat="server" id="myButton" UseSubmitBehavior="False"
              OnClientClient="if (!confirm("Delete?") return;
window.forms[0].submit();" />

+++ Rick ---
--
Rick Strahl
West Wind Technologieswww.west-wind.com/weblog


My GridView has a button with OnClientClick="if (!confirm('Delete?'))
return
false;" which must deletes a record only if the user click on the OK
button
of the confirmation window, but if he clicks on the Cancel button the
record
is also deleted, that is, it is not working.
Could someone help me?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top