我正在设置 PageMethods 以使用 JavaScript 访问服务器端代码。在使用 Firebug 进行调试时,我收到错误提示“PageMethods 未定义”。同时,服务器端代码也没有被触发。
我已经将脚本管理器“EnablePageMethods”设置为 true。我的方法是 public & static 的,并且已经设置了 WebMethod 属性。但我仍然收到上述错误。可能是由于脚本管理器位于顶级主页面中,该主页面有两个子主页面,然后调用了我的页面:
即:
主主页面(带脚本管理器) -> 主要的主页面 -> 次要的主页面 -> 我的页面 -> 我的用户控件(具有 WebMethod)
这是 WebMethod:
[System.Web.Services.WebMethod]
public static void AddNote(string t)
{
int propid = 1;
if (propid > 0)
{
//Call my method
}
}
这是我的JavaScript代码:
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);
}
这是我调用它的位置:
<a href="#" class="btngeneral" onclick="javascript:editNodes(2);">Save</a>