ASP.net AJAX 调用PageMethods实例
2008-03-01 18:27
495 查看
1、新建网站,选择Asp.net Ajax Enabled Web Site 模板
2、 <asp:scrīptManager ID="scrīptManager1" runat="server"/>中增加 EnablePageMethods="True" ,以启用PageMethods
3、编写服务器端方法代码
[System.Web.Services.WebMethod]
public static string GetServerTime(clientValue1,clientValue2)
{
return DateTime.Now.ToString();
}
请注意:a、[System.Web.Services.WebMethod]是必须的,相当于ajax.net中的[ajax.method],b、方法前面要上 public 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[
var ClientValue1,ClientValue2;
function btnGetServerTime_onclick() {
PageMethods.GetServerTime(ClientValue1,ClientValue2,cb_getServerTime,null,[AnyClientValue]); //cb_getServerTime为onSuccess方法
}
function cb_getServerTime(resultFrovServer,[anyClientValue]) { //其中 “anyClientValue”段为可选
document.getElementById("result").innerHTML = resultFrovServer;
}
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[
var ClientValue1,ClientValue2;
function btnGetServerTime_onclick() {
PageMethods.GetServerTime(ClientValue1,ClientValue2,cb_getServerTime,null,[AnyClientValue]); //cb_getServerTime为onSuccess方法
}
function cb_getServerTime(resultFrovServer,[anyClientValue]) { //其中 “anyClientValue”段为可选
document.getElementById("result").innerHTML = resultFrovServer;
}
// ]]>
</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(clientValue1,clientValue2)
{
return DateTime.Now.ToString();
}
}
2、 <asp:scrīptManager ID="scrīptManager1" runat="server"/>中增加 EnablePageMethods="True" ,以启用PageMethods
3、编写服务器端方法代码
[System.Web.Services.WebMethod]
public static string GetServerTime(clientValue1,clientValue2)
{
return DateTime.Now.ToString();
}
请注意:a、[System.Web.Services.WebMethod]是必须的,相当于ajax.net中的[ajax.method],b、方法前面要上 public 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[
var ClientValue1,ClientValue2;
function btnGetServerTime_onclick() {
PageMethods.GetServerTime(ClientValue1,ClientValue2,cb_getServerTime,null,[AnyClientValue]); //cb_getServerTime为onSuccess方法
}
function cb_getServerTime(resultFrovServer,[anyClientValue]) { //其中 “anyClientValue”段为可选
document.getElementById("result").innerHTML = resultFrovServer;
}
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[
var ClientValue1,ClientValue2;
function btnGetServerTime_onclick() {
PageMethods.GetServerTime(ClientValue1,ClientValue2,cb_getServerTime,null,[AnyClientValue]); //cb_getServerTime为onSuccess方法
}
function cb_getServerTime(resultFrovServer,[anyClientValue]) { //其中 “anyClientValue”段为可选
document.getElementById("result").innerHTML = resultFrovServer;
}
// ]]>
</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(clientValue1,clientValue2)
{
return DateTime.Now.ToString();
}
}
相关文章推荐
- 调用PageMethods实例 (ASP.net AJAX)
- ASP.net AJAX 调用PageMethods实例(javascript调用服务器页面方法)
- ASP.net AJAX 调用PageMethods实例
- ASP.net AJAX 调用PageMethods实例(javascript调用服务器页面方法)
- ASP.net AJAX 调用PageMethods实例(javascript调用服务器页面方法)
- ASP.NET中AJAX 调用实例代码
- Jquery + Ajax调用webService实例代码(asp.net)
- ASP.NET Ajax实例—Javascript直接调用服务器C#代码
- Javascript 直接调用服务器C#代码 ASP.NET Ajax实例
- 使用asp.net ajax的PageMethods来让客户端代码调用服务器端的方法
- Javascript 直接调用服务器C#代码 ASP.NET Ajax实例
- ASP.NET Ajax实例—Javascript直接调用服务器C#代码
- Jquery + Ajax调用webService实例代码(asp.net)
- asp.net中,用Ajax实现客户端调用服务器端的函数(PageMethods)
- ASP.NET AJAX中PageMethods方法调用示例
- Asp.net Ajax提供PageMethods调用
- ASP.NET Ajax实例—Javascript直接调用服务器C#代码
- asp.net调用ajax实例
- ASP.NET Ajax实例—Javascript直接调用服务器C#代码
- 使用ASP.NET AJAX调用后台WebService优化网站性能实例