pagemethods is undefined - i believe that everything should be ok

njj56 picture njj56 · Apr 25, 2012 · Viewed 7.2k times · Source

I am trying to use PageMethods to call a function from the server side, but I am getting an error that PageMethods is undefined.

Here is what I have put into the html

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

and here is the function from the code behind.

    <System.Web.Services.WebMethod()> _
Public Function LoadChild(ByVal EntityID, ByVal EntityCat, ByVal lnType, ByVal FullExpand, ByVal lnEquip, ByVal lnTemplate) As String

and here is the call to the function using page methods.

PageMethods.LoadChild(lnEntityID, lnEntityCat, GLOBALEQUIPID, FullExpand, 0);

can anyone see why I would keep getting a PageMethods undefined error? Thank you.

edit - part of the issue may be because the function isnt shared, but when shared is put in, i get this error in about 15 places. - this is refering to the session I am using - here is a snippet of the session being used that is showing an error.

prm4.Value = Session.Item("user_id")

I am getting the word session underlined with the error message below - any suggestions on how to fix this? this could fix the whole issue all together.

Error 305 Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

Answer

Andrey M. picture Andrey M. · Apr 25, 2012

Page method should be a static(C#)/shared(VB.NET) method. Otherwise it does not work. Yes, you can not refer instance property in static method. To get Session object you could use HttpContext.Current.Session.