AJAX调用aspx的静态方法(二)
2009-05-05 23:05
190 查看
上次的文章中说到在页面中异步调用WebServices中的方法,当然也可以异步调用aspx.cs中的静态方法。看下MSDN上的示例代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CallAspxStaticMethod.aspx.cs" Inherits="CallAspxStaticMethod" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml">
6<head runat="server">
7 <title>Using Page Methods with Session State</title>
8 <style type="text/css">
9 </style>
15</head>
16<body>
17 <form id="form1" runat="server">
18 <asp:ScriptManager ID="ScriptManager1"
19 runat="server" EnablePageMethods="true">
20 <Scripts>
21 <asp:ScriptReference Path="JS/PageMethods.js"/>
22 </Scripts>
23 </asp:ScriptManager>
24 </form>
25
26 <center>
27 <table>
28 <tr align="left">
29 <td>Write current date and time in session state:</td>
30 <td>
31 <input type="button"
32 onclick="SetSessionValue('SessionValue', Date())"
33 value="Write" />
34 </td>
35 </tr>
36 <tr align="left">
37 <td>Read current date and time from session state:</td>
38 <td>
39 <input type="button"
40 onclick="GetSessionValue('SessionValue')"
41 value="Read" />
42 </td>
43 </tr>
44 </table>
45 </center>
46
47 <hr/>
48
49 <span style="background-color:Aqua" id="ResultId"></span>
50</body>
51</html>
52
在上面的代码中要注意一点,一定要将ScriptManager的EnablePageMethods属性设为true,这样才可以调用aspx.cs中的方法,ScriptManager下的ScriptReference属性是下面为异步调用写好的js函数
using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.UI;
6using System.Web.UI.WebControls;
7using System.Web.Services;
8
9public partial class CallAspxStaticMethod : System.Web.UI.Page
10
在两个静态方法中要加上[WebMethod]属性。这样才可以被客户端js异步调用.
// PageMethods.js
2
3var displayElement;
4
5// Initializes global variables and session state.
6
12// Gets the session state value.
13
18//Sets the session state value.
19
24// Callback function invoked on successful
25// completion of the page method.
26
33// Callback function invoked on failure
34// of the page method.
35
42if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
GetSessionValue()方法中都有一个PageMethods.GetSessionValue方法,这个方法对应着服务器端的GetSessionValue方法。浏览页面后,我们可以在html源文件中看到关于PageMethods对象的定义.
<script type="text/javascript">
2//<![CDATA[
3PageMethods.registerClass('PageMethods',Sys.Net.WebServiceProxy);
29PageMethods._staticInstance = new PageMethods();
30PageMethods.set_path("/Ajax/CallAspxStaticMethod.aspx");
56//]]>
70</script>
71
在这里我们可以看到GetSessionValue:function(key,succeededCallback, failedCallback, userContext) 函数,在PageMethods.js中给他传入了相应的回调函数。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CallAspxStaticMethod.aspx.cs" Inherits="CallAspxStaticMethod" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml">
6<head runat="server">
7 <title>Using Page Methods with Session State</title>
8 <style type="text/css">
9 </style>
15</head>
16<body>
17 <form id="form1" runat="server">
18 <asp:ScriptManager ID="ScriptManager1"
19 runat="server" EnablePageMethods="true">
20 <Scripts>
21 <asp:ScriptReference Path="JS/PageMethods.js"/>
22 </Scripts>
23 </asp:ScriptManager>
24 </form>
25
26 <center>
27 <table>
28 <tr align="left">
29 <td>Write current date and time in session state:</td>
30 <td>
31 <input type="button"
32 onclick="SetSessionValue('SessionValue', Date())"
33 value="Write" />
34 </td>
35 </tr>
36 <tr align="left">
37 <td>Read current date and time from session state:</td>
38 <td>
39 <input type="button"
40 onclick="GetSessionValue('SessionValue')"
41 value="Read" />
42 </td>
43 </tr>
44 </table>
45 </center>
46
47 <hr/>
48
49 <span style="background-color:Aqua" id="ResultId"></span>
50</body>
51</html>
52
在上面的代码中要注意一点,一定要将ScriptManager的EnablePageMethods属性设为true,这样才可以调用aspx.cs中的方法,ScriptManager下的ScriptReference属性是下面为异步调用写好的js函数
using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.UI;
6using System.Web.UI.WebControls;
7using System.Web.Services;
8
9public partial class CallAspxStaticMethod : System.Web.UI.Page
10
在两个静态方法中要加上[WebMethod]属性。这样才可以被客户端js异步调用.
// PageMethods.js
2
3var displayElement;
4
5// Initializes global variables and session state.
6
12// Gets the session state value.
13
18//Sets the session state value.
19
24// Callback function invoked on successful
25// completion of the page method.
26
33// Callback function invoked on failure
34// of the page method.
35
42if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
GetSessionValue()方法中都有一个PageMethods.GetSessionValue方法,这个方法对应着服务器端的GetSessionValue方法。浏览页面后,我们可以在html源文件中看到关于PageMethods对象的定义.
<script type="text/javascript">
2//<![CDATA[
3PageMethods.registerClass('PageMethods',Sys.Net.WebServiceProxy);
29PageMethods._staticInstance = new PageMethods();
30PageMethods.set_path("/Ajax/CallAspxStaticMethod.aspx");
56//]]>
70</script>
71
在这里我们可以看到GetSessionValue:function(key,succeededCallback, failedCallback, userContext) 函数,在PageMethods.js中给他传入了相应的回调函数。
相关文章推荐
- 运用ajax和jquey直接调用Aspx.cs后台的方法
- C#前台js里面使用ajax调用后台aspx.cs方法
- [JQuery]- 利用jquery的ajax调用后台的WebService公共方法和网页的静态方法
- asp.net利用Ajax和Jquery在前台向后台传参数并返回值,通过aspx.cs中的静态方法+WebMethod进行处理
- 用juery的ajax方法调用aspx.cs页面中的webmethod方法示例
- 用juery的ajax方法调用aspx.cs页面中的webmethod方法示例
- 使用JQuery方式的Ajax调用demo.aspx.cs文件后台的方法
- C# asp.net 在ajax调用的后台静态方法中获取Session
- aspx或ashx里面多个方法 进行ajax调用
- Asp.net AJAX调用后台静态方法总结
- 用juery的ajax方法调用aspx.cs页面中的webmethod方法
- 【Web】【Ajax】Ajax调用Aspx后台方法
- ajax调用aspx.cx页面内的方法
- Ajax三级联动调用aspx后台方法
- asp.net ajax1.0基础回顾(六):调用ASPX页面方法 推荐
- Ajax三级联动调用aspx后台方法
- JQuery .ajax方法直接调用aspx后台代码
- jQuery Ajax 方法调用 Asp.Net WebService 以及调用aspx.cs中方法的详细例子
- 调用Ajax返回500错误的解决方法
- 【静态和非静态方法】调用静态和非静态方法【原创】