Link to home
Start Free TrialLog in
Avatar of jayrod
jayrodFlag for United States of America

asked on

disable button in .net 2.0 web page

I have a .net 2.0 form that disables the submit button after clicked.  THis works just fine:

btnSubmit.Attributes.Add("onClick", "javascript:document.getElementById('" + btnSubmit.ClientID + "').value='Please wait...'; javascript:document.getElementById('" + btnSubmit.ClientID + "').disabled = true;")

However, I have validation controls being used also.  If the page is not valid, the button is still disbaled.  How do I keep the button enabled if the page is not valid?  I have tried the following:

If Page.IsValid Then
            '//processing here
            btnSubmit.Text = "Continue Mortgage Application"
            btnSubmit.Enabled = True
        End If

which does not seem to be working
Avatar of stanscott2

There is no enabled property, only disabled. You need to use this:

btnSubmit.disabled = False;
Avatar of jayrod


No - that is incorrect.  I am using VS.Net 2005, Visual Basic and there is no disabled property - only enabled.

Anyway - that does not matter as the code is not even getting to that point because of the validation controls.  Thanks!
Avatar of Tanglin05

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jayrod


Hi Todd!
thanks so much!  That works great!  However, the validation seems to be firing twice now?  I have tried putting the JS code above in the HEAD and the BODY and it still seems to fire twice, showing the popup validation alert twice.  Is there any explanationn for this - hopefully it will be an easy fix :)  Thanks again so much!
Well...technically it will fire twice now. Once when you call the validation routine (Page_ClientValidate()) and once when the "normal" page life cycle calls the validation routine.

The reason we call the Page_ClientValidate is to ensure that the Page_IsValid property is properly updated with the current valid state of the page. I think your options are to either move away from using alerts to display validation warnings (using page labels instead) or to try to "disable" the normal Page_ClientValidate call.

If you want to go the route of disabling the normal Page_ClientValidate call, you may be able to "unwire" the function after you call it in your button click so that when it's called by the validators it does nothing. For example:

function disableButton(e){
   // Do nothing if client validation is not active
   if (typeof(Page_Validators) == "undefined")  return;

   if (typeof(Page_ClientValidate) == 'function') {
      if (Page_ClientValidate() == false) {
         //Set the Page_ClientValidate function to a "dummy" function so it doesn't fire again
         Page_ClientValidate = function(){return false;};
         return false;
         //check client-side IsValid variable set by Page_ClientValidate
            //Do you button disabling here
            e.disabled = true;
            e.value = 'Please wait...';

Give that a try or check out the links I supplied for some additional ideas.