views:

107

answers:

3

I have forms with payment in ASP.NET site, when user clicks on asp:Button sometime he submits few times. I want to disable this functionality without harming current ASP.NET site ( by that i mean no fancy jQuery, unless it is very basic and will not interfere with ASP.NET built-in validation)

thanks!!!

+6  A: 

You can easily do this on the button in client side:

onClientClick="this.disabled=true;"

You can also use the approach from Muhammad with this:

onClientClick="if(Page_ClientValidate()){this.disabled=true;}"

If this is still not working, you can initiate the postback yourself before disabling the button:

onClientClick="if(Page_ClientValidate()){__doPostBack;this.disabled=true;}"
Oded
@Oded, very smart solution, like it...
Muhammad Akhtar
@Oded, cool thanks!
eugeneK
@Oded, doesn't work in Chrome after ASP.NET Validation been made.
eugeneK
@eugeneK - "doesn't work" isn't very descriptive. What doesn't work?
Oded
@Oded, the Button doesn't click after validation
eugeneK
@eugeneK - and other browswers?
Oded
not xhtml-valid!
Andreas Niedermair
@Andreas Niedermair - where did you see that this is xhtml?
Oded
read http://www.w3schools.com/xhtml/xhtml_syntax.asp: disabled="disabled"
Andreas Niedermair
@Andreas Niedermair - I will rephrase. What in the question makes you think it has anything to do with XHtml? How do you know this is not simply html 4 or 5?
Oded
@Oded: with html4: http://www.w3.org/TR/html401/interact/forms.html#h-17.12.1. so anyway, not really correct. i fear this causes chrome to not accept it ... :(
Andreas Niedermair
@Oded and @Andreas Niedermair, do you understand what fails? If Button is disabled on client and there are ASP:Validators it fails.
eugeneK
oh. the whole approach fails ... sry for that ...
Andreas Niedermair
@Oded, it still doesn't work with even with Muhammad's fix for Client Validation.Don't know what the problem. button just becomes unclickable after few failed validations once i set proper login details
eugeneK
@eugeneK - you can try to postback before disabling the button. See my updated answer.
Oded
@Oded, still fails even with your 3rd solution.
eugeneK
@eugeneK - still the same, or any different? All browsers or only chrome?
Oded
@Oded, i do my tests in Chrome only. I don't know about other browsers.
eugeneK
A: 

call this js funtion on clientclick of button

function disableButton() {
if(Page_ClientValidate())
  {
        document.getElementById('<%= btnSubscribe.ClientID %>').disabled = true;
   }
    }

before diable button, you need to check page validation.

Muhammad Akhtar
@Muhammad Akhtar, doesn't work in Chrome after ASP.NET Validation been made. Possibly other browsers
eugeneK
no valid xhtml!
Andreas Niedermair
@Muhammad Akhtar, thanks. I will check this solution as well. Just with little bit modifications. Page_ClientValidate() was in fact useful.
eugeneK
@eugeneK, if this soltion help, then don't forget to mark as Answer :)
Muhammad Akhtar
Don't know what the problem. button just becomes unclickable after few failed validations once i set proper login details
eugeneK
+1  A: 

you might go for blockUI. it's really nice, as it blocks the complete ui, not only the button - so that a user can't click on another button (eg. you want to save ... takes some time ... user clicks on delete meanwhile)

Andreas Niedermair
Problem with that approach is. There are two things ASP:Button does.1.JS validation using ASP.NET Validators.2.Triggering OnClick=""this script can be executed on client, thus will fail JS validation.
eugeneK
Andreas Niedermair
by the way - there's another approach: why not redifine the submit-handler of the form. if the form gets submitted by the last instance (eg your client-validation) you can block the ui, can't you?
Andreas Niedermair
@Andreas Niedermair, i'm not familiar with ClientValidation methods which are triggered once validated
eugeneK
then i would suggest, to implement your own eventHandler for form-submit. this would occure if any button is clicked and any validiation succeeded.
Andreas Niedermair