100分了,我想问一个关于在ajax中调用WebMethod和PageMethod的问题! js里面要调用的方法,应该是public类型的,你的是吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 小例子:前台代码:<%@ 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(); } }} 高手幫我看一下這個問題怎解決http://topic.csdn.net/u/20071024/21/2368ffaa-9a54-48f3-8f86-95ad2e0138bd.html 非常感谢这个帖子,终于知道pagemethod如何调用了。 正常模式运行的Page,是一个Page实例,所有控件都以实例形式存在于Page中。PageMethod必须是static的,因此执行时并没有Page实例,没有任何控件,只能做WebService那样的纯粹数据处理,不能使用Page模型。 我也感谢ayooxi(路人),纠结了半天的问题终于找到答案了 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.方法名(参数,回调函数)。 在windows7 iis上部署asp.net网站后发现SqlParameter传参出现了问题 如下 顶贴者有赏 后台cs怎样调用前台写好的js 数据库求和查询后,怎么将结果显示到页面? 模板页主题样式丢失 怎么取得框架里面用户所输入的内容? <求助>关于Session的取值和判断 如何在代码页控制authentication中的内容? 谁知道大连哪有vs.net培训的好点的地方? asp.net 报错 快下班了还没有解决的问题ObjectDataSource提示错误 VS2005下DIV定位
前台代码:<%@ 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();
}
}
}
http://topic.csdn.net/u/20071024/21/2368ffaa-9a54-48f3-8f86-95ad2e0138bd.html
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.方法名(参数,回调函数)。