1

我有一个 EmployeeMaster 页面,我在其中输入 empcode、empname 等。输入 empcode 后,当我单击 empname 文本框时,如果不存在这样的 empcode,我想显示成功图像,如果存在 empcode,则显示失败图像。

有没有办法使用 jquery ajax 方法来显示这个?

下面是我如何尝试调用文本框更改功能。

 $(document).ready(function(){
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    //Raised before processing of an asynchronous postback starts and the postback request is sent to the server.
    prm.add_beginRequest(BeginRequestHandler);
    // Raised after an asynchronous postback is finished and control has been returned to the browser.
    prm.add_endRequest(EndRequestHandler);    
    AutoComp();//function for autofill textbox and it works perfectly.     
    $("#<%=txtEmpCode.ClientID %>").change(checkEmpCode);//calling textbox change  function

});
function checkEmpCode() {
    alert('hai');
}

这里没有显示警报。我该如何解决这个问题....

4

1 回答 1

0

这是您调用 Web 服务的 javascript,它检查 empcode 是否重复,如果重复,该方法将返回 0

<script type="text/javascript"> 
  $(function() {
    $("#empcode").change(checkEmpCode);
  });

  function checkEmpCode() {
    $.ajax({
      type: "POST",
      url: "Service.asmx/CheckEmpCode",
      data: "{empcode: '" + $('#empcode').val() + "'}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(response) {            
        if (response.d != "0") {
          $("#failureimage").show();
        }
        else{
          $("#successimage").show();
        }

      }
    });
  }

</script> 

您的网络服务将具有此方法

[WebMethod]
public int CheckEmpCode(string empcode)
{
  string connect = @"Server=SERVER;Database=Database;Trusted_Connection=True;";
  string query = "SELECT COUNT(*) FROM Employee WHERE empcode = @empcode";
  using(SqlConnection conn = new SqlConnection(connect))
  {
    using(SqlCommand cmd = new SqlCommand(query, conn))
    {
      cmd.Parameters.AddWithValue("empcode", empcode);
      conn.Open();
      return (int)cmd.ExecuteScalar();
    }
  }
}
于 2012-09-27T18:28:44.603 回答