Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
417 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...