OnClientClick with confirm doesn't work

Discussion in 'ASP .Net Web Controls' started by Mauricio Pires, Jan 8, 2009.

  1. 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?
    Mauricio Pires, Jan 8, 2009
    1. Advertisements

  2. Mauricio Pires

    Jester98x Guest

    Hi Mauricio,

    Had same problem myself some time ago.

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

    Hope this helps.
    Jester98x, Jan 12, 2009
    1. Advertisements

  3. Mauricio Pires

    rstrahl Guest

    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 ---
    rstrahl, Jan 14, 2009
  4. Mauricio Pires

    Jester98x Guest


    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.


    Jester98x, Jan 21, 2009
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.