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

标签 javascript jquery asp.net

我正在尝试检查用户是否是 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/

相关文章:

javascript - 用 stroke-dasharray 和事件颜色填充划分半圆

javascript - 我可以使用什么查询从选定键值的本地存储中获取数据

c# - ASP.NET MVC 4 跨字段或属性验证

javascript - Img.svg 到 svg 标签

javascript - Raphael.js 拖动比例会导致奇怪的跳跃行为

javascript - Angular 数字被解释为字符串、表格分页

jquery - 允许出现在任何地方的 HTML5 元素

c# - 解析器错误消息 : 'FCR2.abs' is not allowed here because it does not extend class 'System.Web.UI.Page'

c# - 如何在 Entity Framework 4 Entitydesigner 中正确设置两个对象之间的关联?

javascript - 检查 onClick react 的值