1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
[WebMethod]
public string Echo(string toEcho)
{
return toEcho;
}
[WebMethod]
public void RaiseException()
{
throw new InvalidOperationException("You did the wrong thing!");
}
[WebMethod]
public void DummyWait(int milliseconds)
{
System.Threading.Thread.Sleep(milliseconds);
}
[WebMethod]
public string WaitAndEcho(int milliseconds, string toEcho)
{
System.Threading.Thread.Sleep(milliseconds);
return toEcho;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Atlas: Page Methods</title>
<style type="text/css">
body, input {
font-family:Verdana, Tahoma;
font-size:12px;
}
#content {
border:solid 5px #808080;
width:80%;
margin:auto;
padding:10px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager id="scriptManager" runat="server"></atlas:ScriptManager>
<div id="content">
<h2>A closer look at Page methods</h2>
<p>
The text typed into the textbox will be echoed by a server-side method:
</p>
<div>
<input type="text" id="txtToEcho" value="Type some text" />
<input type="button" id="btnEcho" value="Echo" onclick="btnEcho_Click()" />
</div>
<p>
Click the "Raise Error" button to throw a server-side exception from a page method:
</p>
<div>
<input type="button" id="btnError" value="Raise Error" onclick="btnError_Click()" />
</div>
<p>
Clicking on the "Do Timeout" button will make an asynchronous request timeout before
the page method returns:
</p>
<div>
<input type="button" id="btnTimeout" value="Do Timeout" onclick="btnTimeout_Click()" />
</div>
<p>
To test the abort action, click on "Send Request", then on "Abort" to force an abort
of the page method call:
</p>
<div>
<input type="button" id="btnSendRequest" value="Send Request" onclick="btnSendRequest_Click()" />
<input type="button" id="btnAbort" value="Abort" onclick="btnAbort_Click()" />
</div>
<p style="border:dashed 1px Red">
<span id="lblInfo"> </span>
</p>
</div>
</form>
<script type="text/javascript">
<!--
Sys.Application.load.add(Application_Load);
var txtToEcho;
var lblInfo;
var request;
var dateFormatString = 'hh:mm:ss:ff';
function Application_Load() {
txtToEcho = new Sys.UI.TextBox($('txtToEcho'));
lblInfo = new Sys.UI.Label($('lblInfo'));
}
function btnEcho_Click() {
request = PageMethods.Echo(txtToEcho.get_text(), onComplete, onTimeout, onError, onAbort, new Date());
}
function btnError_Click() {
request = PageMethods.RaiseException(onComplete, onTimeout, onError);
}
function btnTimeout_Click() {
request = PageMethods.DummyWait(3000, onComplete, onTimeout, onError, onAbort, new Date(), 2000);
}
function btnSendRequest_Click() {
request = PageMethods.WaitAndEcho(5000, txtToEcho.get_text(), onComplete, onTimeout, onError, onAbort, new Date());
}
function btnAbort_Click() {
if(request) {
request.abort();
}
}
function onComplete(result, response, context) {
var sentDate = context;
var recvDate = new Date();
lblInfo.set_text('Request sent at: ' +
sentDate.toFormattedString(dateFormatString) +
'\r\nResponse received at: ' +
recvDate.toFormattedString(dateFormatString));
alert(result);
}
function onTimeout(request, context) {
var sentDate = context;
lblInfo.set_text('Request sent at: ' + sentDate.toFormattedString(dateFormatString));
alert('Oops... a timeout occurred. Please retry later.');
debug.dump(request);
}
function onError(objError, response, context) {
if(objError) {
alert(objError.get_message() + '\r\nError: ' + objError.get_exceptionType() );
}
else {
alert('Response status code: ' + response.get_statusCode());
}
}
function onAbort() {
alert('Request aborted.');
}
//-->
</script>
</body>
</html>
|