1.在顶端(Form中即可)增加ScriptManager(全局脚本控制器,必须有),以及其中的EnablePageMethods属性:
<asp:scriptManager id="sm1" runat="server" EnablePageMethods=“True" />2.服务器端函数:
[System.Web.Services.WebMethod] <=必须
Public static String getServerTime(){
return DateTime.Now.ToString();
}
3.页面中:
<input type="button" value="返回服务器时间” id“btnTime" onClick="return btnTime_onClick()" />
<span id="resultSpan">4.客户端Javascript:
<script language="javascript" text="text/javascript">
function btnTime_onClick(){
PageMethods.getServerTime(cb_getServerTime);
}
function cb_getServerTime(result){
document.getElementById("resultSpan").InnerHTML = result;
}
</script>5.此例可在客户端取得服务器时间
6.PageMethods.静态方法名(参数列表,成功执行的回调函数, 失败的回调函数, 上下文对象)

本文介绍如何在ASP.NET中使用AJAX实现跨页调用功能,包括设置ScriptManager启用PageMethods属性,定义服务器端方法getServerTime获取当前时间,并通过客户端JavaScript调用该方法将结果显示在页面上。
&spm=1001.2101.3001.5002&articleId=51871108&d=1&t=3&u=ab0205c1c7a6475f99c9661038e9bbcb)
5225

被折叠的 条评论
为什么被折叠?



