cancelling a postback on a button with javascript

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Re: cancelling a postback on a button with javascript

    Chris:

    Try using the onSubmit event of the Form element to cancel the submission,
    as opposed to the onClick event.

    <script language=javascript>
    function DoSubmit()
    {
    if (confirm("Sure?"))
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    </script>

    <form method=post onSubmit=return DoSubmit();>

    Returning false will stop the form from posting.

    HTH
    --
    Elliot M. Rodriguez, MCSD
    *** It would take 227 cans of Mountain Dew to kill me***



    "Chris" <chrisb@papex.com> wrote in message
    news:13f701c34f83$bbd2cb90$3501280a@phx.gbl...
    > Does any one know a simple way to cancel a serverside
    > postback on a button in asp.net using javascript? Right
    > now, I indicate that I want the the button to postback to
    > the server and be handle by a certain event. However, I
    > also want to the give the user an opportunity to cancel
    > the request. Right now, I have both an onclick event for
    > the button which uses the confirm function in javascript:
    > i.e. (confirm('are you sure you want to delete?');
    >
    > The problem. Regardless if the "ok" or "cancel" buttons
    > are pressed, the postback event for the button is fired.
    > Looking at the source code I can see why. My onclick
    > function is processed first, and then the ___dopostback
    > ('mybutton',''); is fired. How can I stop this from
    > happening?
    >
    > Any thoughts or suggestions would be greatly appreciated.
    >
    > Thanks,
    >
    > Chris...

    Bob Guest

  2. Similar Questions and Discussions

    1. custom web control +client-side javascript + postback
      Hello, I am trying to create a new Web Control (ASP.NET 1.1) that contains among other textboxes. The content of these textboxes (runat=server)...
    2. DataGrid contains no data after postback of edit button click...
      This is specifically what is going on in my application:...
    3. serverside button and javascript
      You have to add a return to the code the Alex gave you deleteQuote.Attribute.Add("onclick", "return(confirm_delete());")
    4. button created using attributes.add to run script in html...fires with each page postback
      Hi, I have the following script in an aspx html: <script language="javascript"> function pop_window() { var confirmWin = null; confirmWin =...
    5. JavaScript Access to Button in form tags (webcontrol or html button)
      Hello, I have a button called LoadBtn, which exists in <form name="Form1" runat=server></form> tags. I then have javascript loaded outside of...
  3. #2

    Default Re: cancelling a postback on a button with javascript

    Hi Bob,

    Unfortunately, the onsubmit method doesn't get invoked when using a
    standard postback. For example: Form1.submit(), which is part of the
    ___dopostack function generated by asp.net doesn't invoke the onsubmit()
    method. Too damn bad though, it was a good idea, and I thought I had my
    problem solved. Thanks for the help...

    Chris...



    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Chris Barrow Guest

  4. #3

    Default Re: cancelling a postback on a button with javascript

    Still solved - change the button from a WebControl to an HTMLInputControl
    and specify runat=server. That should do the trick.

    --
    Elliot M. Rodriguez, MCSD
    *** It would take 227 cans of Mountain Dew to kill me***



    "Chris Barrow" <chrisb@papex.com> wrote in message
    news:Opbtro7TDHA.2316@tk2msftngp13.phx.gbl...
    > Hi Bob,
    >
    > Unfortunately, the onsubmit method doesn't get invoked when using a
    > standard postback. For example: Form1.submit(), which is part of the
    > ___dopostack function generated by asp.net doesn't invoke the onsubmit()
    > method. Too damn bad though, it was a good idea, and I thought I had my
    > problem solved. Thanks for the help...
    >
    > Chris...
    >
    >
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    Elliot M. Rodriguez Guest

  5. #4

    Default Re: cancelling a postback on a button with javascript

    Hi Elliot,

    It worked! Thank you. I actually had the button declared as an
    HTMLInputButton within the vb code-behind, but on the .aspx side as
    <input type="button", rather than "submit". Changing the type of button,
    caused the "DoSubmit" function to be invoked when the button was
    clicked. Pressing the cancel button in the confirm dialog box cancels
    the postback.

    Thanks again for your help, and sorry for calling you Bob! I must have
    been drinking too much mountain dew the first time I replied to you.

    Chris...


    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Chris Barrow Guest

  6. #5

    Default Re: cancelling a postback on a button with javascript

    Just as a note, you can stop post back from any type of button if you just
    use an if-then javascript for the confirmation:

    MyButton.Attributes.Add("onClick", "javascript:if(!confirm('" &
    MessageToDisplay & "')) return false;")


    --
    S. Justin Gengo, MCP
    Web Developer

    Free code library at:
    [url]www.aboutfortunate.com[/url]

    "Out of chaos comes order."
    Nietzche
    "Chris Barrow" <chrisb@papex.com> wrote in message
    news:OFXagnEUDHA.2252@TK2MSFTNGP12.phx.gbl...
    > Hi Elliot,
    >
    > It worked! Thank you. I actually had the button declared as an
    > HTMLInputButton within the vb code-behind, but on the .aspx side as
    > <input type="button", rather than "submit". Changing the type of button,
    > caused the "DoSubmit" function to be invoked when the button was
    > clicked. Pressing the cancel button in the confirm dialog box cancels
    > the postback.
    >
    > Thanks again for your help, and sorry for calling you Bob! I must have
    > been drinking too much mountain dew the first time I replied to you.
    >
    > Chris...
    >
    >
    > *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    S. Justin Gengo Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139