Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PageMethods is not defined

I am setting up PageMethods to access server side code using javascript. I am receiving the error "PageMethods is not defined" when i debug using firebug. the server side code is not being fired as well.

I have setup my script manager "EnablePageMethods" to true. My method is public & static and it has the WebMethod attribute set up as well. Still i m receiving the above error. Can it be because of the script manager being in a top level master page which has two child master pages and then my page is called:

i.e.

Main Master Page (with script manager) -> Primary master page -> Secondary master page -> My page -> My User control (having the WebMethod)

Is it possible tht the hierarchy is causing this issue?

This is the webmethod

 [System.Web.Services.WebMethod]
public static void AddNote(string t)
{
    int propid = 1; 
    if (propid > 0)
    {
     //Call my method
    }
}

this is my javascript code:

  function editNodes(t) {
    alert('test1');
    alert(t);
    PageMethods.AddNote(t,OnSuccess,OnFailure);
    alert('method called');
}
function OnSuccess() { 
    alert('Success'); 
    if (!navigator.appName == 'Microsoft Internet Explorer') {
        window.location.href = window.location.href;
    }

}
function OnFailure(error) {
    alert('Error:' + error);
}    

this is where I am calling it:

   <a href="#" class="btngeneral" onclick="javascript:editNodes(2);">Save</a>
like image 514
ria Avatar asked Nov 28 '22 07:11

ria


2 Answers

Page Methods don't work in either Master Pages or User Controls.

If you move the method up one level to you page it should work.

Never really been able to get a definitive answer on this one.

I suspect it has do with the fact that aspx pages inherit from the Page class and the ascx controls inherit from the UserControl class. As to why of that I am not too sure.

Personally I would use a generic handler (.ashx) page and call that through javascript.

like image 91
heads5150 Avatar answered Dec 19 '22 01:12

heads5150


set EnablePageMethods="true"

 <ajaxToolkit:ToolkitScriptManager ID="scriptManager" 
                               runat="server" 
                               AsyncPostBackTimeout="99999999"
                               EnablePageMethods="true" />

i hope this will work for you

like image 24
BrainCoder Avatar answered Dec 19 '22 02:12

BrainCoder