Enable/Disable Asp.net Image button during postback
<p>I am trying to check if user is <code>available</code> or not in the <code>database</code> for that I have kept a button named "<code>Check availability</code>".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</p>

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

<p>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 <code>duplicate field</code>.</p>

<p>And these are the many ways I have worked out but none worked out for me.</p>

<pre><code>.enabled = true;

.disabled = false;

document.getElementById('&lt;%= button.ClientID %&gt;').disabled = true;
document.getElementById('&lt;%= button.ClientID %&gt;').disabled = false;

$('#ButtonId').prop("disabled", true); -&gt;&gt; disabled
$('#ButtonId').prop("disabled", false); -&gt;&gt; Enabled
</code></pre>

<p>Here is my code:</p>

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

            } else {
                $("#&lt;% =txtUserName.ClientID %&gt;").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
                $.post("LoginHandler.ashx", { uname: $("#&lt;% =txtUserName.ClientID %&gt;").val() }, function (result) {
                    if (result == "1") {

                        $("#&lt;% =txtUserName.ClientID %&gt;").addClass('notavailablecss').fadeTo(900, 1);
                      document.getElementById('&lt;%= btnSignUp.ClientID %&gt;').enabled = false;
                    }
                    else if (result == "0") {
                        $("#&lt;% =txtUserName.ClientID %&gt;").addClass('availablecss').fadeTo(900, 1);
                        document.getElementById('&lt;%= btnSignUp.ClientID %&gt;').enabled = true;
                    }
                    else {
                        $("#&lt;% =txtUserName.ClientID %&gt;").addClass('notavailablecss').fadeTo(900, 1);

                    }
                });
            }
        });

        $("#&lt;% =btnavailable.ClientID %&gt;").ajaxError(function (event, request, settings, error) {
            alert("Error requesting page " + settings.url + " Error:" + error);
        });
    });
&lt;/script&gt;
</code></pre>

<p>This is my handler code:</p>

<pre><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
</code></pre>

<p>Updated Code for Check Availability Click:</p>

<pre><code>  If txtUserName.BackColor = Drawing.Color.Red Then
        btnSignUp.Enabled = False
    ElseIf txtUserName.BackColor = Drawing.Color.Green Then
        btnSignUp.Enabled = True
    End If
</code></pre>
