简体   繁体   English

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

[英]Enable/Disable Asp.net Image button during postback

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

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. 现在我有一个注册页面,如果用户存在,用户填写表单我需要禁用SignUp button ,我正在使用“ Image button ”。

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 . 当我在页面加载期间禁用“图像”按钮(即“注册”按钮)时以及用户填写表单后,虽然用户可用,但页面正在刷新并将信息作为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

Try using: disabled=disabled 尝试使用: disabled=disabled

EDIT 编辑

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

If the button is an Asp.Net button: 如果按钮是Asp.Net按钮:

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

Or... 要么...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM