PageMethods未在ASPX页面中定义

Jon*_*ood 17 javascript asp.net ajax pagemethods

我正在看一些我只能假设一次工作的旧代码.

MyPage.aspx:

function GetCompanyList(officeId) {
    var companyList = document.getElementById('<%= CompanyDropDown.ClientID %>');
    if (companyList.length == 0)
        PageMethods.GetCompanyList(officeId, OnGetCompanyList);
    else
        EditCompany();
}
Run Code Online (Sandbox Code Playgroud)

和:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
Run Code Online (Sandbox Code Playgroud)

代码背后:

[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public IEnumerable<CompanyMinimum> GetCompanyList(int officeId) {
    return (
        from c in Repository.Query<Company>()
        where !c.IsDeleted && c.TypeEnumIndex == (short)CompanyRelationshipType.Hotel
        select new CompanyMinimum() {
            id = c.Id,
            desc = c.Description
        }
    ).ToList();
}
Run Code Online (Sandbox Code Playgroud)

但是,PageMethods.GetCompanyList()在第一个代码段的调用中,Chrome会报告:

PageMethods未定义

谁能看到改变了什么来防止这种情况发生?

注意:我发现了类似的问题,但它们似乎都与此代码无关,无法在母版页或用户控件中使用,这不是这里的情况.

Jon*_*ski 25

EnablePageMethods实际上只有一个方法相互作用Page被子类public,static及归属作为WebMethod.

GetCompanyList有2个,也只是需要static.

[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static IEnumerable<CompanyMinimum> GetCompanyList(int officeId) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

而且,我怀疑正在发生的事情是,PageMethods如果找不到任何具有全部3的方法,它将离开未定义的客户端.


Kar*_*son 5

您可以通过 jQuery 调用 ASP.NET AJAX 页面方法,如下所示:

$.ajax({
    type: "POST",
    url: "PageName.aspx/MethodName",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        // Do something interesting here.
    }
});
Run Code Online (Sandbox Code Playgroud)