使用asp.net web应用程序无法专注于文本

使用asp.net web应用程序无法专注于文本,asp.net,ajax,Asp.net,Ajax,在下面的代码中,我有一个文本框,当我关注文本框时,它应该调用服务器端功能,没有警报消息。请帮助我解决问题。我添加了web服务引用。请任何人帮助我找到并解决问题 focus.ascx: <html> <head> <script type="text/javascript"> $(document).ready(function () { $("#<%=txtField.ClientID%>").bind("focus", f

在下面的代码中,我有一个文本框,当我关注文本框时,它应该调用服务器端功能,没有警报消息。请帮助我解决问题。我添加了web服务引用。请任何人帮助我找到并解决问题

focus.ascx:

<html>
<head>
<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); 
                },
                error: function (xhr) {
                }
            });
        });
    });
</script>
</head>
<body>

<form id="frm" runat="server">
<asp:TextBox ID="txtField" runat="server" AutoPostBack="true" ClientIDMode="Static"  ></asp:TextBox></form>
</body></html>

按照以下步骤,您将获得所需的输出

Ajax函数调用
函数CallAjaxRequest(){
var Simplefailuredata={};
Simplefailuredata.Id=1;
Simplefailuredata.Comments='Comments-1';
Simplefailuredata.得分=500.25;
Simplefailuredata.Adjustment=700.25;
Simplefailuredata.ShutdownData=新数组();
Simplefailuredata.ShutdownData[0]=新对象({Id:2,说明:“Desc-1”,CausedShutdown:true,ShutdownType:“ShutdownType-1”});
Simplefailuredata.ShutdownData[1]=新对象({Id:5,说明:“Desc-2”,CausedShutdown:false,ShutdownType:“ShutdownType-2”});
var object=JSON.stringify({simplefailuredata:simplefailuredata});
$.ajax({
类型:“POST”,
url:“Default2.aspx/GetResponse”,
contentType:'application/json;charset=utf-8',
数据:对象,
数据类型:“json”,
缓存:false
});
}
代码隐藏
public部分类Default2:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
[System.Web.Services.WebMethod]
公共静态字符串GetResponse(simplefailuredata simplefailuredata)
{
返回“”;
}
}
公共类simplefailuredata
{ 
公共int Id;
公共字符串注释;
公众双倍得分;
公共双重调整;
公共列表关闭数据;
} 
公共类关闭数据
{ 
公共int Id{get;set;}
公共字符串说明{get;set;}
public bool CausedShutdown{get;set;}
公共字符串关闭类型{get;set;}
}  

客户端需要注意的事项。如果文本框的
clientdmode
属性设置为
Static
,则不需要服务器端表达式。另外,
将为您提供网页的物理路径,而不是网页的url。您需要设置ajax请求的内容类型。将javascript代码更改为

$(document).ready(function () {
    $("#txtField").bind("focus", function () {
        $.ajax({
            type: "POST",
            url: "/path_if_any/page.aspx/txtField_GotFocus",
            data: "{'foo':'whatever'}",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data) {
                alert(data.d); 
            },
            error: function (xhr) {
            }
        });
    });
});
然后page方法需要一个名为
foo
的属性的对象。此对象可以是

public class MyObject
{
    public string foo{get;set;}
}
最后更改page方法签名以接收上述对象

    [WebMethod]
    public static string txtField_GotFocus(MyObject obj)
    {
        string foo = obj.foo;

        return "awesome, it works!";
    }

我想通过焦点而不是在asp:TextBox中单击来呼叫服务器端,我没有收到任何警报,而且它不工作。请检查我的url“focus.ascx/txtField\u GotFocus”。url应该引用您的网页,而不是您的用户控件
$(document).ready(function () {
    $("#txtField").bind("focus", function () {
        $.ajax({
            type: "POST",
            url: "/path_if_any/page.aspx/txtField_GotFocus",
            data: "{'foo':'whatever'}",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data) {
                alert(data.d); 
            },
            error: function (xhr) {
            }
        });
    });
});
public class MyObject
{
    public string foo{get;set;}
}
    [WebMethod]
    public static string txtField_GotFocus(MyObject obj)
    {
        string foo = obj.foo;

        return "awesome, it works!";
    }