在回发期间启用/禁用Asp.net图像按钮

时间:2011-09-23 13:37:43

标签: javascript jquery asp.net

我正在尝试检查available中的用户是否为database,因为我保留了一个名为“Check availability”的按钮。一旦用户点击它,他就可以检查是否该名称是否存在于数据库中,如果“存在”则将文本框背景颜色更改为红色,如果不存在则将文本框背景颜色更改为“绿色”

现在我有一个注册页面,如果用户存在,用户填写表单我需要禁用我正在使用“SignUp button”的Image button

所以当用户可用时,我无法禁用它。 当我在页面加载期间禁用“图像”按钮(即“注册”按钮)时以及用户填写表单后,虽然用户可用,但页面正在刷新并以duplicate field的形式将信息提交到数据库。 / p>

这些是我解决过的很多方法,但没有一个方法可以帮助我。

.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

这是我的代码:

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

这是我的处理程序代码:

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

更新了检查可用性代码点击:

  If txtUserName.BackColor = Drawing.Color.Red Then
        btnSignUp.Enabled = False
    ElseIf txtUserName.BackColor = Drawing.Color.Green Then
        btnSignUp.Enabled = True
    End If

2 个答案:

答案 0 :(得分:1)

尝试使用:disabled=disabled

修改

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

答案 1 :(得分:1)

如果按钮是Asp.Net按钮:

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

或者...

$("[id$=ButtonId]").prop("disabled", true);