在另一个论坛上,有人抱怨 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 网站。