使用asp.net web appln关注文本框时出错

时间:2014-01-31 09:02:19

标签: asp.net ajax

在下面的代码中我有一个文本框,当焦点来到我的文本框时,它应该使用ajax调用服务器端代码隐藏文件,但在我的情况下,我可以获得成功警报,但它没有移动到服务器端并返回值pls帮助我修复问题。 代码:

<script type="text/javascript">
      $(document).ready(function () {
          $("#<%=txtField.ClientID%>").bind("focus", function () {
              $.ajax({
                  type: "POST",
                  url: "<%=Request.FilePath%>/txtField_GotFocus",
                  data: "{foo:'whatever'}",
                  success: function (msg) {
                      alert(msg); //awesome, it works!
                  },
                  error: function (xhr) {
                  }
              });
          });
      });
</script>
 <asp:TextBox ID="txtField" runat="server" AutoPostBack="true" ClientIDMode="Static"  OnTextChanged="txtField_TextChanged"></asp:TextBox>

代码隐藏

public static string txtField_GotFocus()
        {
            string foo = HttpContext.Current.Request["foo"];
            //code...
            return "awesome, it works!";
        }

1 个答案:

答案 0 :(得分:0)

您的功能应如下所示

[WebMethod]
public static string txtField_GotFocus()
{
        string foo = HttpContext.Current.Request["foo"];
        //code...
        return "awesome, it works!";
 }

以下是一个示例链接

  1. http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/