【问题标题】:How to show success message using jquery ajax method in asp.net on textbox change?如何在 asp.net 中使用 jquery ajax 方法在文本框更改上显示成功消息?
【发布时间】:2012-09-27 15:36:20
【问题描述】:

我有一个 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');
}

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

【问题讨论】:

标签: jquery asp.net ajax


【解决方案1】:

这里是你调用 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();
    }
  }
}

【讨论】:

  • 抱歉回复晚了。我已经写了change函数,但是这个函数不工作。我用警报消息测试了这个函数。我已经更新了上面的代码
  • 是的,我终于明白了。非常感谢您的精彩回答。没有您,我想我将无法实现这一目标...
猜你喜欢
  • 1970-01-01
  • 2016-12-27
  • 2017-03-21
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多