Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable and disable button using javascript and asp.net

I am checking if user exists in database if exists I am showing message as "existed user" and then I need to disable the signup button if not I need to enable it.

I am unable to enable and disable the sign Up button.

Can anyone help me in this issue?

Here is my code:

 <script type="text/javascript">
     $(function () {
         $("#<% =btnavailable.ClientID %>").click(function () {
             if ($("#<% =txtUserName.ClientID %>").val() == "") {
                 $("#<% =txtUserName.ClientID %>").removeClass().addClass('notavailablecss').text('Required field cannot be blank').fadeIn("slow");

             } else {
                 $("#<% =txtUserName.ClientID %>").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
                 $.post("LoginHandler.ashx", { uname: $("#<% =txtUserName.ClientID %>").val() }, function (result) {
                     if (result == "1") {
                         $("#<% =txtUserName.ClientID %>").addClass('notavailablecss').fadeTo(900, 1);
                       document.getElementById(#<% =btnSignUp.ClientID %>').enabled = false;
                     }
                     else if (result == "0") {
                         $("#<% =txtUserName.ClientID %>").addClass('availablecss').fadeTo(900, 1);
                        document.getElementById('#<% =btnSignUp.ClientID %>').enabled = true;
                     }
                     else {
                         $("#<% =txtUserName.ClientID %>").addClass('notavailablecss').fadeTo(900, 1);
                     }
                 });
             }
         });

         $("#<% =btnavailable.ClientID %>").ajaxError(function (event, request, settings, error) {
             alert("Error requesting page " + settings.url + " Error:" + error);
         });
     });
</script>
like image 844
coder Avatar asked Sep 22 '11 12:09

coder


People also ask

How do I disable a button in JavaScript?

To disable a button using only JavaScript you need to set its disabled property to false . For example: element. disabled = true . And to enable a button we would do the opposite by setting the disabled JavaScript property to false .

How do I disable and enable a button in CSS?

To make the disabled button, we will use the Pure CSS class “pure-button-disabled” with the class “pure-button”. We can also create a disabled button using disabled attribute.


2 Answers

Try this...

document.getElementById('<%= button.ClientID %>').disabled = true;

OR

document.getElementById('<%= button.ClientID %>').disabled = false;
like image 80
sharmila Avatar answered Oct 13 '22 13:10

sharmila


JavaScript:

 function Enable() {
      $("#btnSave").attr('disabled', false);                
    }
 function Disable() {
       $("#btnSave").attr('disabled', true);
    }  

ASPX Page:

 <asp:Button runat="server" ID="btnSave" Text="Save" UseSubmitBehavior="false" OnClientClick="if(Page_ClientValidate('Validation')){javascript:Disable();}" ValidationGroup="Validation"/>

Code Behind:

ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Disable", "javascript:Disable();", True)
like image 42
Raj Avatar answered Oct 13 '22 12:10

Raj