12

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>
2
  • And your problem is? How can someone help you now? Commented Sep 22, 2011 at 12:30
  • My problem is unable to disable and enable the signup button.If username existed I need to disable it and if not I need to enable it. Commented Sep 22, 2011 at 12:31

9 Answers 9

11

Unfortunately your problem is something as small as the difference between enabled and disabled

.enabled = true;

Should be :

.disabled = false;

Sign up to request clarification or add additional context in comments.

3 Comments

Yes I have done that as shown above but when the user is available and I click SignUp button it's refreshing the page.
I see you're using POST so i assume you're just missing event.preventDefault() in your event handler on your button.
add your submit click handler to your original post.
10

You can play with this:

$('#ButtonId').prop("disabled", true); ->> disabled
$('#ButtonId').prop("disabled", false); ->> Enabled

2 Comments

as much as i love jquery too, i dont think OP is using it.
sorry, habit of looking at the tags at a glance ;)
9

Try this...

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

OR

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

2 Comments

Yeh I have tried the same thing as mentioned in the above code but not working.
I tried setting a "var" to this value and then setting the "disabled", but nothing happened. When I used the above code it all worked. I believe it has something to do with a "var" variable not being understood as a button, where the above code is still understood that way.
7

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)

Comments

2

u can just set visibility of your button to false visibility="false"

2 Comments

Yes I have done that too but when the user clicks the show availability then if the user exists in the database then I need to make the signUp button enabled which is not happenning
for that u have to use some if else statement with some conditions and in c# code buttoneName.visibility=true or false
1

The .disabled does work, have you used break point to make sure your code was even being reached ? Your conditional if / else's may not be returning your expected values.

Comments

1

I do this:

Disable:

    var myButton = document.getElementById('<%= this.myButton.ClientID %>');
    $(myButton).attr('disabled', 'disabled');  

Enable:

    $(myButton).removeAttr('disabled');

Comments

0

To enable and disable the buttons using JavaScript, this is what should be done-

Example:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="a(); return false;"/>
<asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="b(); return false;" />

and

<script type="text/javascript">
   function a() {
       alert('1');
       document.getElementById('<%=Button1.ClientID %>').disabled = true;
       document.getElementById('<%=Button2.ClientID %>').disabled = false;
      }
   function b() {
       alert('2');
       document.getElementById('<%=Button2.ClientID %>').disabled = true;
       document.getElementById('<%=Button1.ClientID %>').disabled = false;
      }
 </script>

NOTE: While other posts had similar code, they fail because of the reload on the page. So to avoid the reload a return false should be added like OnClientClick="a(); return false;"

Comments

0

This JavaScript code should work.

document.getElementById("<%=btnSignUp.ClientID%>").disabled = true; //To disable the button

document.getElementById("<%=btnSignUp.ClientID%>").disabled = false;//To enable the button

Your code should look like

<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%>").disabled = true;
                     }
                     else if (result == "0") {
                         $("#<% =txtUserName.ClientID %>").addClass('availablecss').fadeTo(900, 1);
                        document.getElementById("<%=btnSignUp.ClientID%>").disabled = false;
                     }
                     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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.