0

I am trying to check if user is available or not in the database for that I have kept a button named "Check availability".Once when user clicks that he can checks whether the name exists in the database or not and changes the textbox background color to red if "exists" and "green" if not

Now I have a registration page where the user fills the form if the user exists I need to disable the SignUp button which I am using the "Image button" for it.

So What happens is I am unable to disable it while the user is available. When I am disabling the Image button(ie..the SignUp button) during page load and after the user fills the form though the user is available the page is refreshing and submitting the information to the database as a duplicate field.

And these are the many ways I have worked out but none worked out for me.

.enabled = true;

.disabled = false;

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

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

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>

This is my handler code:

Public Class LoginHandler : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    Dim uname As String = context.Request("uname")
    Dim result As String = "0"
    If uname IsNot Nothing Then
        result = CheckUnAvailable(uname)
    End If
    context.Response.Write(result)
    context.Response.[End]()
End Sub
Public Function CheckUnAvailable(ByVal name As String) As String
    Dim result As String = "0"
    Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("HDIConnectionString").ConnectionString)
    Try
        con.Open()
        Dim com As New SqlCommand("Select Username from Registration where Username=@UserName", con)
        com.Parameters.AddWithValue("@UserName", name)
        Dim uname As Object = com.ExecuteScalar()
        If uname IsNot Nothing Then
            result = "1"
        End If
    Catch
        result = "error"
    Finally
        con.Close()
    End Try
    Return result
End Function
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
        Return False
    End Get
End Property

Updated Code for Check Availability Click:

  If txtUserName.BackColor = Drawing.Color.Red Then
        btnSignUp.Enabled = False
    ElseIf txtUserName.BackColor = Drawing.Color.Green Then
        btnSignUp.Enabled = True
    End If
3
  • I don't know if this would help you, but in one of my projects we developed a script, which prevent buttons from sending more then one postback. Basically what it did, it rewrote the javascript in href attribute, so that no matter how many times you clicked, only one postback would be sent. My suggestion to you would be to implement similar behaviour - if user clicks the "Check availability" button, temporarily remove the postback javascript from your form's button, and after your request is done, put the postback javascript back in place. Commented Sep 23, 2011 at 13:47
  • Yes I agree just now I was searching the same thing on the web.And it is for resubmitting the same details but in my case is the user searches for the name and finds it is not availble and he tries to submit the form and it accepts but I need to disable at that time. Commented Sep 23, 2011 at 13:51
  • Yes I have disabled as I have shown in my updated code but it's not happening. Commented Sep 23, 2011 at 13:53

2 Answers 2

1

If the button is an Asp.Net button:

$('#<%= button.ClientID %>').prop("disabled", true);

Or...

$("[id$=ButtonId]").prop("disabled", true);
Sign up to request clarification or add additional context in comments.

4 Comments

Note: .prop is a fairly new feature of jQuery. Are you using 1.6.x?
It's an ImageButton but it's accepting with this also and Updating the data.
I believe the .prop is a 1.6.x feature. Upgrade or do NOT use .prop with 1.4.
Even I use above 1.6 nothing happened.
1

Try using: disabled=disabled

EDIT

Try: $('#ButtonId').removeProp("disabled");

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.