ASP.net AJAX 调用PageMethods实例
来源:互联网 发布:大数据研判 编辑:程序博客网 时间:2024/06/09 04:25
1、新建网站,选择Asp.net Ajax Enabled Web Site 模板(如图)
2、 <asp:scrīptManager ID="scrīptManager1" runat="server"/>中增加 EnablePageMethods="True" ,以启用PageMethods
[System.Web.Services.WebMethod]
public static string GetServerTime()
{
return DateTime.Now.ToString();
}
请注意:a、[System.Web.Services.WebMethod]是必须的,相当于ajax.net中的[ajax.method],b、方法前面要上static
4、页面设计,内容如下
<div>
<input type="button" value="服务器时间" id="btnGetServerTime" ōnclick="return btnGetServerTime_onclick()" />
<span id="result" />
</div>
5、编写客户端javascrīpt
<scrīpt language="javascrīpt" type="text/javascrīpt">
// <!CDATA[
function btnGetServerTime_onclick() {
PageMethods.GetServerTime(cb_getServerTime);
}
function cb_getServerTime(result) {
document.getElementById("result").innerHTML = result;
}
6、执行程序,单击"服务器时间" 按钮就可返回服务器的时间并显示在result中。
附:
一、客户端代完整代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<scrīpt language="javascrīpt" type="text/javascrīpt">
// <!CDATA[
function btnGetServerTime_onclick() {
PageMethods.GetServerTime(cb_getServerTime);
}
function cb_getServerTime(result) {
document.getElementById("result").innerHTML = result;
}
// ]]>
</scrīpt>
</head>
<body>
<form id="form1" runat="server">
<asp:scrīptManager ID="scrīptManager1" runat="server" EnablePageMethods="True" />
<div>
<input type="button" value="服务器时间" id="btnGetServerTime" ōnclick="return btnGetServerTime_onclick()" />
<span id="result" />
</div>
</form>
</body>
</html>
二、服务器端完整代码
using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
[System.Web.Services.WebMethod]
public static string GetServerTime()
{
return DateTime.Now.ToString();
}
}
由于我做的时候是没用AJAX的模板,所以在web.config里加了一个节点,网上都这么说
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
可是还是不成,单击按钮时,提示:未知的运行时错误,搞了很久,终于在web.config里再加了一个节点
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
终于成功~~
- ASP.net AJAX 调用PageMethods实例
- 调用PageMethods实例 (ASP.net AJAX)
- ASP.net AJAX 调用PageMethods实例(javascript调用服务器页面方法)
- ASP.NET JS调用PageMethods
- ASP.NET Ajax、PageMethods完整代码
- 使用asp.net ajax的PageMethods来让客户端代码调用服务器端的方法
- asp.net中,用Ajax实现客户端调用服务器端的函数(PageMethods)
- ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例
- ASP.NET在调用js方法时出现Uncaught ReferenceError:PageMethods is not defined的解决方法
- ASP.net AJAX代码实例。
- Asp.net Jquery Ajax 实例
- ASP.net AJAX代码实例
- ASP.net AJAX代码实例
- Asp.net Jquery Ajax 实例
- Asp.net Jquery Ajax 实例
- asp.net 调用jquery ajax
- ajax 调用asp.net 后台
- ASP.NET WebService+Ajax调用
- 备份sql
- error: stdio.h: 没有那个文件或目录
- error when compile apache "No recognized SSL/TLS toolkit detected"
- 有关VC6的几个调试技巧整理
- 配置管理软件(configuration management software)介绍
- ASP.net AJAX 调用PageMethods实例
- c# delegate(委托)与多线程
- 注册表控件的设计时授权
- 在UI上使用BackgroundWorker
- (转)拦截其它程序的网络数据封包
- Groovy Tip 22 魔幻数组 二
- SEO学习心得
- ddddddddddddddddddddd
- 致自己的第一份工作