我正在尝试检查用户是否是 available
或不在database
中为此,我保留了一个名为“Check availability
”的按钮。一旦用户单击该按钮,他就可以检查该名称是否存在于数据库中,如果“存在”,则将文本框背景颜色更改为红色,如果不存在,则将文本框背景颜色更改为“绿色”
现在我有一个注册页面,如果用户存在,用户可以在其中填写表单,我需要禁用 SignUp button
我正在使用“Image button
”。
所以发生的情况是,当用户可用时我无法禁用它。
当我在页面加载期间禁用图像按钮(即注册按钮)时,以及在用户填写表单后(尽管用户可用),页面会刷新并将信息作为 duplicate field
提交到数据库。 .
这些是我尝试过的很多方法,但没有一个适合我。
.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
最佳答案
如果按钮是 Asp.Net 按钮:
$('#<%= button.ClientID %>').prop("disabled", true);
或者...
$("[id$=ButtonId]").prop("disabled", true);
关于javascript - 回发期间启用/禁用 Asp.net 图像按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7529825/