尽管复制了示例代码,但未定义PageMethods

时间:2012-10-09 08:26:34

标签: javascript asp.net

在另一个论坛上有人抱怨没有定义PageMethods。有人提供了一个答案说......“看,这就是你让他们工作的方式”。所以我复制了他们的代码并试了一下。我仍然看到PageMethods没有定义。

页面如下所示:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageMethods.aspx.cs" Inherits="PageMethods" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body onload="GetFromServer();">
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true"></asp:ScriptManager>
<div>

</div>
</form>
<script type="text/javascript">
function GetFromServer()
{
PageMethods.GetHello(OnGetHelloComplete);
}
function OnGetHelloComplete(result, userContext, methodName)
{
alert("Result: " + result + "\n" +
"Context: " + userContext + "\n" +
"Method name: " + methodName);
}
</script>
</body>
</html>

这就是背后的代码。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Web.Script.Services;
public partial class PageMethods : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string GetHello()
{
    return "Hello From Server!";
}
}

当您运行该页面时,会发生javascript错误 - 未定义“PageMethods”。

这是一个.net 2.0网站。

1 个答案:

答案 0 :(得分:0)

我认为这个需要.NET 3.5。我刚刚在快速演示中使用了您的确切代码,我可以毫无困难地调用GetHello方法。