2011-09-23 81 views
0

我想检查用户是否available或不在database因为我已经保存了一个名为“Check availability”的按钮。一旦用户点击它,他可以检查名称是否存在于数据库中,如果不存在则将文本框背景颜色更改为红色回发期间启用/禁用Asp.net图像按钮

现在我有一个注册页面,用户填写表单时,如果用户存在,我需要禁用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 [email protected]", 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 
+0

我不知道,如果这会帮助你,但在我的一个项目中,我们开发了一个脚本,它可以防止按钮发送多个回发。基本上它做了什么,它在_href_属性中重写了javascript,所以无论您单击多少次,都只会发送一个回发。 我对你的建议是实现类似的行为 - 如果用户点击“检查可用性”按钮,暂时从表单的按钮中删除回发JavaScript,并在请求完成后,将回发的JavaScript放回原处。 – Trogvar

+0

是的,我刚刚同意我在网上搜索同样的东西。它是用于重新提交相同的细节,但在我的情况是用户搜索名称,发现它是不可用的,他试图提交表单和它接受,但我需要在那个时候禁用。 – coder

+0

是的,我已禁用,因为我已经在我的更新代码中显示,但它没有发生。 – coder

回答

1

使用尝试:disabled=disabled

编辑

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

+0

不适用于disabled =禁用 – coder

1

如果按钮是一个Asp.Net按钮:

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

或者......

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

注意:.prop是jQuery的一项相当新的功能。你在使用1.6.x吗? –

+0

这是一个ImageButton,但它也接受并更新数据。 – coder

+0

我正在使用jquery 1.4 min.js – coder