Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
And your problem is? How can someone help you now? – Job Vermeulen Sep 22 '11 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. – coder Sep 22 '11 at 12:31

5 Answers

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

.enabled = true;

Should be :

.disabled = false;

share|improve this answer
Yes I have done that as shown above but when the user is available and I click SignUp button it's refreshing the page. – coder Sep 22 '11 at 12:34
I see you're using POST so i assume you're just missing event.preventDefault() in your event handler on your button. – f0x Sep 22 '11 at 12:38
add your submit click handler to your original post. – f0x Sep 22 '11 at 12:46

Try this...

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

OR

document.getElementById('<%= button.ClientID %>').disabled = false;
share|improve this answer
Yeh I have tried the same thing as mentioned in the above code but not working. – coder Sep 22 '11 at 12:38

You can play with this:

$('#ButtonId').prop("disabled", true); ->> disabled
$('#ButtonId').prop("disabled", false); ->> Enabled
share|improve this answer
as much as i love jquery too, i dont think OP is using it. – f0x Sep 22 '11 at 12:36
It's accepting though user is not available. – coder Sep 22 '11 at 12:37
@f0x By the code OP posted, jQuery is at hand! – Adrian Carneiro Sep 22 '11 at 13:23
sorry, habit of looking at the tags at a glance ;) – f0x Sep 22 '11 at 13:26

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

share|improve this answer
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 – coder Sep 22 '11 at 12:55
for that u have to use some if else statement with some conditions and in c# code buttoneName.visibility=true or false – prakash kumar jha Sep 23 '11 at 18:40

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;"

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.