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.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466

4 Answers4

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.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • 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. – Karl Anderson Aug 14 '13 at 21:23
  • @KarlAnderson: Trying to confirm that now. – Jonathan Wood Aug 14 '13 at 21:28
  • 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! – Jonathan Wood Aug 14 '13 at 21:34
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.
    }
});
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • 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. – Jonathan Wood Aug 14 '13 at 21:11
  • 1
    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? – Karl Anderson Aug 14 '13 at 21:13
  • Same result on IE. Looks like it might be working on an older version of Firefox. – Jonathan Wood Aug 14 '13 at 21:19
  • Before you change the method to be static, does it give you the same error if you use jQuery's `.ajax()` function? – Karl Anderson Aug 14 '13 at 21:24
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);
cgonzalez
  • 46
  • 2
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.

Enkode
  • 4,515
  • 4
  • 35
  • 50