上次的文章中说到在页面中异步调用WebServices中的方法,当然也可以异步调用aspx.cs中的静态方法。看下MSDN上的示例代码

CallAspxStaticMethod.aspx
1
<%@ 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
body
{ font: 11pt Trebuchet MS;
10
font-color: #000000;
11
padding-top: 72px;
12
text-align: center }
13
.text
{ font: 8pt Trebuchet MS }
14
</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函数

CallAspxStaticMethod.aspx.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Web;
5
using System.Web.UI;
6
using System.Web.UI.WebControls;
7
using System.Web.Services;
8
9
public partial class CallAspxStaticMethod : System.Web.UI.Page
10

{
11
[WebMethod]
12
// Get session state value.
13
public static string GetSessionValue(string key)
14
{
15
return (string)HttpContext.Current.Session[key];
16
}
17
18
[WebMethod]
19
// Set session state value.
20
public static string SetSessionValue(string key, string value)
21
{
22
HttpContext.Current.Session[key] = value;
23
return (string)HttpContext.Current.Session[key];
24
}
25
26
27
protected void Page_Load(object sender, EventArgs e)
28
{
29
30
}
31
}
32
在两个静态方法中要加上[WebMethod]属性。这样才可以被客户端js异步调用.

PageMethods.js
1
// PageMethods.js
2
3
var displayElement;
4
5
// Initializes global variables and session state.
6
function pageLoad()
{
7
displayElement = $get("ResultId");
8
PageMethods.SetSessionValue("SessionValue", Date(),
9
OnSucceeded, OnFailed);
10
}
11
12
// Gets the session state value.
13
function GetSessionValue(key)
{
14
PageMethods.GetSessionValue(key,
15
OnSucceeded, OnFailed);
16
}
17
18
//Sets the session state value.
19
function SetSessionValue(key, value)
{
20
PageMethods.SetSessionValue(key, value,
21
OnSucceeded, OnFailed);
22
}
23
24
// Callback function invoked on successful
25
// completion of the page method.
26
function OnSucceeded(result, userContext, methodName)
{
27
if (methodName == "GetSessionValue")
{
28
displayElement.innerHTML = "Current session state value: " +
29
result;
30
}
31
}
32
33
// Callback function invoked on failure
34
// of the page method.
35
function OnFailed(error, userContext, methodName)
{
36
if (error !== null)
{
37
displayElement.innerHTML = "An error occurred: " +
38
error.get_message();
39
}
40
}
41
42
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
GetSessionValue()方法中都有一个PageMethods.GetSessionValue方法,这个方法对应着服务器端的GetSessionValue方法。浏览页面后,我们可以在html源文件中看到关于PageMethods对象的定义.

Code
1
<script type="text/javascript">
2
//<
var PageMethods = function()
{
4
PageMethods.initializeBase(this);
5
this._timeout = 0;
6
this._userContext = null;
7
this._succeeded = null;
8
this._failed = null;
9
}
10
PageMethods.prototype =
{
11
_get_path:function()
{
12
var p = this.get_path();
13
if (p) return p;
14
else return PageMethods._staticInstance.get_path();},
15
GetSessionValue:function(key,succeededCallback, failedCallback, userContext)
{
16
/// <param name="key" type="String">System.String</param>
17
/// <param name="succeededCallback" type="Function" optional="true" mayBeNull="true"></param>
18
/// <param name="failedCallback" type="Function" optional="true" mayBeNull="true"></param>
19
/// <param name="userContext" optional="true" mayBeNull="true"></param>
20
return this._invoke(this._get_path(), 'GetSessionValue',false,
{key:key},succeededCallback,failedCallback,userContext); },
21
SetSessionValue:function(key,value,succeededCallback, failedCallback, userContext)
{
22
/// <param name="key" type="String">System.String</param>
23
/// <param name="value" type="String">System.String</param>
24
/// <param name="succeededCallback" type="Function" optional="true" mayBeNull="true"></param>
25
/// <param name="failedCallback" type="Function" optional="true" mayBeNull="true"></param>
26
/// <param name="userContext" optional="true" mayBeNull="true"></param>
27
return this._invoke(this._get_path(), 'SetSessionValue',false,
{key:key,value:value},succeededCallback,failedCallback,userContext); }}
28
PageMethods.registerClass('PageMethods',Sys.Net.WebServiceProxy);
29
PageMethods._staticInstance = new PageMethods();
30
PageMethods.set_path = function(value)
{
31
PageMethods._staticInstance.set_path(value); }
32
PageMethods.get_path = function()
{
33
/// <value type="String" mayBeNull="true">The service url.</value>
34
return PageMethods._staticInstance.get_path();}
35
PageMethods.set_timeout = function(value)
{
36
PageMethods._staticInstance.set_timeout(value); }
37
PageMethods.get_timeout = function()
{
38
/// <value type="Number">The service timeout.</value>
39
return PageMethods._staticInstance.get_timeout(); }
40
PageMethods.set_defaultUserContext = function(value)
{
41
PageMethods._staticInstance.set_defaultUserContext(value); }
42
PageMethods.get_defaultUserContext = function()
{
43
/// <value mayBeNull="true">The service default user context.</value>
44
return PageMethods._staticInstance.get_defaultUserContext(); }
45
PageMethods.set_defaultSucceededCallback = function(value)
{
46
PageMethods._staticInstance.set_defaultSucceededCallback(value); }
47
PageMethods.get_defaultSucceededCallback = function()
{
48
/// <value type="Function" mayBeNull="true">The service default succeeded callback.</value>
49
return PageMethods._staticInstance.get_defaultSucceededCallback(); }
50
PageMethods.set_defaultFailedCallback = function(value)
{
51
PageMethods._staticInstance.set_defaultFailedCallback(value); }
52
PageMethods.get_defaultFailedCallback = function()
{
53
/// <value type="Function" mayBeNull="true">The service default failed callback.</value>
54
return PageMethods._staticInstance.get_defaultFailedCallback(); }
55
PageMethods.set_path("/Ajax/CallAspxStaticMethod.aspx");
56
PageMethods.GetSessionValue= function(key,onSuccess,onFailed,userContext)
{
57
/// <param name="key" type="String">System.String</param>
58
/// <param name="succeededCallback" type="Function" optional="true" mayBeNull="true"></param>
59
/// <param name="failedCallback" type="Function" optional="true" mayBeNull="true"></param>
60
/// <param name="userContext" optional="true" mayBeNull="true"></param>
61
PageMethods._staticInstance.GetSessionValue(key,onSuccess,onFailed,userContext); }
62
PageMethods.SetSessionValue= function(key,value,onSuccess,onFailed,userContext)
{
63
/// <param name="key" type="String">System.String</param>
64
/// <param name="value" type="String">System.String</param>
65
/// <param name="succeededCallback" type="Function" optional="true" mayBeNull="true"></param>
66
/// <param name="failedCallback" type="Function" optional="true" mayBeNull="true"></param>
67
/// <param name="userContext" optional="true" mayBeNull="true"></param>
68
PageMethods._staticInstance.SetSessionValue(key,value,onSuccess,onFailed,userContext); }
69
//]]>
70
</script>
71
在这里我们可以看到GetSessionValue:function(key,succeededCallback, failedCallback, userContext) 函数,在PageMethods.js中给他传入了相应的回调函数。