PageMethods is not Defined in ASPX Page
Asked Answered
G

4

23

I'm looking at some old code that I can only assume worked at one time.

MyPage.aspx:

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

And:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

Code behind:

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

But at the call to PageMethods.GetCompanyList() in the first snippet, Chrome reports:

PageMethods is not defined

Can anyone see what has changed to prevent this from working?

Note: I've found similar questions but they all seemed related to this code not working in master pages or user controls, which isn't the case here.

Geometrize answered 14/8, 2013 at 21:7 Comment(0)
S
29

EnablePageMethods actually only interacts with methods of a Page subclass that are public, static, and attributed as a WebMethod.

GetCompanyList has 2 of those and just also needs to be static.

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

And, I suspect what's happening is that it's leaving PageMethods undefined client-side if it doesn't find any methods that have all 3.

Struck answered 14/8, 2013 at 21:17 Comment(3)
You are definitely right about the need for the method to be static, but I am not sure it would complain about the JavaScript proxy being undefined if there were no valid page methods.Regal
@KarlAnderson: Trying to confirm that now.Geometrize
Yes, this appears to be the issue. I found where it was changed from static to non-static some time ago when we refactored part of the code. While I don't have a static version working yet, I now get a different error than the one I had. Thanks!Geometrize
R
5

You can invoke ASP.NET AJAX Page Methods via jQuery, like this:

$.ajax({
    type: "POST",
    url: "PageName.aspx/MethodName",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        // Do something interesting here.
    }
});
Regal answered 14/8, 2013 at 21:9 Comment(4)
Sorry, I pasted the wrong code in my second snippet. Yes, EnablePageMethods is set to true. I'm familiar with $.ajax() from working with MVC, but still it would be nice to get this code working without a rewrite.Geometrize
There may be an issue with whichever version of Chrome you are trying and the JavaScript generated by ASP.NET AJAX, jQuery will provide you with a much better cross-browser experience, but I understand your desire to get it working. Does it work in IE and/or Firefox?Regal
Same result on IE. Looks like it might be working on an older version of Firefox.Geometrize
Before you change the method to be static, does it give you the same error if you use jQuery's .ajax() function?Regal
M
2

maybe you are using Routing in your pages. then must be set real path after call PageMethods:

PageMethods.set_path("<%=ResolveUrl("~/YourPage.aspx")%>");
PageMethods.YourMethod(param, OnSuccess, OnError);
Mosra answered 4/10, 2013 at 19:58 Comment(0)
D
0

One answer from another solution that I think should be represented is if this error occurs on your server but not locally is to place the empty MyPage.aspx placeholder file and now it works on the production server too.

Diggins answered 23/7, 2015 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.