PageMethods没有定义定义、PageMethods

2023-09-04 00:46:27 作者:无关善良

我设立PageMethods访问服务器端code使用JavaScript。我收到错误PageMethods没有定义当我调试使用萤火虫。在服务器端code不被解雇了为好。

我有我的设置脚本管理器的EnablePageMethods为真。我的方法是公共和放大器;静态的,它设置好了WebMethod属性。尽管如此I M收到上述错误。难道是因为在一个顶级主网页,其中有两个子母版页,然后我的网页被称为脚本管理器是:

主要母版页(使用脚本管理器) - >主主控页面 - >中的母版页 - >我的页面 - >我的用户控件(具有的WebMethod)

是否有可能THT层次结构是造成这个问题?

这是将WebMethod

  [System.Web.Services.WebMethod]
公共静态无效AddNote(串T)
{
    INT PROPID = 1;
    如果(PROPID大于0)
    {
     //调用我的方法
    }
}
 

这是我的javascript code:

 函数editNodes(T){
    警报('测试1');
    警报(T);
    PageMethods.AddNote(T,的onSuccess,OnFailure);
    警报('方法称为');
}
功能的onSuccess(){
    警报(成功);
    如果(!navigator.appName ==Microsoft Internet Explorer的'){
        window.location.href = window.location.href;
    }

}
功能OnFailure(错误){
    警报('错误:'+误差);
}
 
为什么你参加书法比赛却没有获得好成绩

这就是我称之为:

 < A HREF =#级=btngeneral的onclick =JavaScript的:editNodes(2);>保存< / A>
 

解决方案

页面方法并不在任一母版页或用户控制工作。

如果你上一级的方法取决于你网页它应该工作。

从来没有真正能够得到一个明确的答案就这一个。

我怀疑它的事实,aspx页面从Page类继承和ASCX控制从用户控件类继承做。至于为什么,我也不太清楚。

我个人使用一个通用处理器(ashx的)页面,并呼吁通过JavaScript的。

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>

解决方案

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.