如何在asp.net中使用jquery ajax方法在文本框更改中显示成功消息?

时间:2022-09-15 11:49:24

I've got a EmployeeMaster page where i enter empcode,empname etc.After entering empcode,when i click on the empname textbox,i want to show a success image if no such empcode exists and a failure image if empcode exists..

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

Is there any way to show this using jquery ajax method ?

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

Heres how i've tried to call the textbox change function.

以下是我试图调用文本框更改功能的方法。

 $(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');
}

Here alert is not displaying.How can i solve this issue....

这里警报没有显示。我怎么能解决这个问题....

1 个解决方案

#1


0  

Here is the javascript where you call the web service which check if the empcode is duplicate, the method will return 0 if duplicate

这是您调用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> 

Your webservice will have this method

您的Web服务将具有此方法

[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();
    }
  }
}

#1


0  

Here is the javascript where you call the web service which check if the empcode is duplicate, the method will return 0 if duplicate

这是您调用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> 

Your webservice will have this method

您的Web服务将具有此方法

[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();
    }
  }
}