js里面要调用的方法,应该是public类型的,你的是吗?

解决方案 »

  1.   

    小例子:
    前台代码:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AjaxFrame.WebForm1" %><!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>
        <script type="text/javascript">
        
          var pa1 = "Time is" ;
          function go() {
            
                PageMethods.cs(pa1,js);
          }
          
          function js(cs)
          {
            alert(cs);
          }
                </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input type="button" onclick="go();" value="submits" />
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
        </div>
        </form>
    </body>
    </html>
    -----------------------------------------------------------------------------------------------------------------------
    后台代码:using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Linq;
    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;namespace AjaxFrame
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {        }        [System.Web.Services.WebMethod]        public static string cs(string pa1)
            {
                return pa1+DateTime.Now.ToString();
            }
        }
    }
      

  2.   

    高手幫我看一下這個問題怎解決
    http://topic.csdn.net/u/20071024/21/2368ffaa-9a54-48f3-8f86-95ad2e0138bd.html
      

  3.   

    非常感谢这个帖子,终于知道pagemethod如何调用了。
      

  4.   

    正常模式运行的Page,是一个Page实例,所有控件都以实例形式存在于Page中。PageMethod必须是static的,因此执行时并没有Page实例,没有任何控件,只能做WebService那样的纯粹数据处理,不能使用Page模型。
      

  5.   

    我也感谢ayooxi(路人),纠结了半天的问题终于找到答案了
      

  6.   

    PageMethods方法是在页面或者js文件中调用当前页面后台代码的方法,这种方法有以下几个步骤,一、在页面上添加<asp:ScriptManagerProxy></asp:ScriptManagerProxy>控件,如果出现PageMethods未定义时可在页面上或者js文件中引入以下代码即可var PageMethods = function() {
    PageMethods.initializeBase(this);
    this._timeout = 0;
    this._userContext = null;
    this._succeeded = null;
    this._failed = null;
    }二、需调用的后台方法必须是public 和static的,同时为方法添加[WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]属性,使用方法PageMethods.方法名(参数,回调函数)。