3

I'm trying to write code that will disable submit button (or all submit buttons) on the page to avoid double postback.

I thought of generating my own postback javascript function (and inject postback javascript using GetPostbackEventReference) but maybe there are some better ways to do this? Or maybe there is some other way to avoid double postbacks?

Update:

I also want to figure out the best place to call javascript, e.g. if I call following script in the onclick button handler then button's server click event won't be wired up.

$('input[type=submit]').attr('disabled', 'disabled')
Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
  • See my answer for another question if you don't want to fix this on a button-by-button basis: http://stackoverflow.com/a/28844217/787757 – sparebytes Mar 05 '15 at 15:26

3 Answers3

4

http://greatwebguy.com/programming/dom/prevent-double-submit-with-jquery/

This should help you

   1. $('form').submit(function(){  
   2.     $(':submit', this).click(function() {  
   3.         return false;  
   4.     });  
   5. });  
Shawn
  • 7,235
  • 6
  • 33
  • 45
  • I used answer from the link which was in the link you proposed :) http://henrik.nyh.se/2008/07/jquery-double-submission – Andrew Bezzub Nov 16 '10 at 14:35
2

jQuery:

$('input[type=submit]').attr('disabled', 'disabled')

Otherwise, iterate through all document.getElementByTagName('input')

May be best to do this only for child nodes of the form that was submitted, though?

Chris Broadfoot
  • 5,082
  • 1
  • 29
  • 37
  • That wouldn't stop a) ` – Gareth Nov 16 '10 at 13:39
  • I also need some gentle way to run above code. If I execute it using OnClientClick then during postback button click sever event is not wired up. – Andrew Bezzub Nov 16 '10 at 13:42
0

How about this:

$('input[type=button]').click(function() {
    $('input[type=button]').each(function() {
        $(this).attr('disabled', 'disabled')
    });
});

Anytime any button is clicked, every button on the form becomes disabled.

Chris Conway
  • 16,269
  • 23
  • 96
  • 113