为什么这个页面方法失败了?

时间:2009-10-20 00:19:02

标签: c# ajax pagemethods

我的脚本管理器的属性enablepagemethods设置为true,但是,出于某种原因,这提醒我,我失败了。

 public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod()]
    public static string test()
    {
        return "q343242342342";
    }
}

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
  function callMethod() {
      PageMethods.test(onSuccess, onFailure);
  }

  function onSuccess(result) {
      alert(result.d);
  }

  function onFailure(error) {
      alert('fail');
  } 
         </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server"  EnablePageMethods="True">
 </asp:ScriptManager>
 <div>
    <asp:Button ID="Button1" OnClientClick="callMethod()" runat="server" Text="Button"/>

1 个答案:

答案 0 :(得分:1)

这段代码对我有用 - 无法确切地说明你的问题在哪里你没有你的代码在runat = server块中(假设你只是从后面的代码复制)。但是这个确切的代码应该可行 - 我想。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<html>
<head runat="server">
<title>Sample Page</title>
<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethod()]
    public static string test()
    {
        return "q343242342342";
    }

</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
  function callMethod() {
      PageMethods.test(onSuccess, onFailure);
  }

  function onSuccess(result) {
      alert(result);
  }

  function onFailure(error) {
      alert('fail');
  } 
         </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
 </asp:ScriptManager>
 <input type="button" id="btn" value="Click Me" onclick="callMethod();"  />
 </form>
 </body>
 </html>