Академический Документы
Профессиональный Документы
Культура Документы
NET AJAX
Extensions
Jeff Prosise
Cofounder, Wintellect
www.wintellect.com
Architecture
ASPX ASMX
Application Services
Bridge
Server
Controls
Asynchronous
Communications
ASP.NET 2.0
DragOverlay-
ScriptManager UpdatePanel
Extender
ScriptManager- Update-
Proxy Progress ProfileService
Timer
ScriptManager
Starting point for ASP.NET AJAX pages
What does ScriptManager do?
Downloads JavaScript files to client
Enables partial-page rendering using
UpdatePanel
Provides access to Web services via
client-side proxies
Manages callback timeouts, provides
error handling options and infrastructure,
and more
Every page requires one
ScriptManager Schema
<asp:ScriptManager ID="ScriptManager1" Runat="server"
EnablePartialRendering="true|false"
EnablePageMethods="true|false"
AsyncPostBackTimeout="seconds"
AsyncPostBackErrorMessage="message"
AllowCustomErrorsRedirect="true|false"
OnAsyncPostBackError="handler"
EnableScriptGlobalization="true|false"
EnableScriptLocalization="true|false"
ScriptMode="Auto|Inherit|Debug|Release"
ScriptPath="path">
<Scripts>
<!-- Declare script references here -->
</Scripts>
<Services>
<!-- Declare Web service references here -->
</Services>
</asp:ScriptManager>
Script References
"Name" references load script resources
Animated GIF
Canceling an Update
<asp:UpdateProgress DisplayAfter="500" ...>
<ProgressTemplate>
<b>Working...</b>
<asp:Button ID="CancelButton" Runat="server" Text="Cancel"
OnClientClick="cancelUpdate(); return false" />
</ProgressTemplate>
</asp:UpdateProgress>
<script type="text/javascript">
function cancelUpdate()
{
var obj = Sys.WebForms.PageRequestManager.getInstance();
if (obj.get_isInAsyncPostBack())
obj.abortPostBack();
}
</script>
Demo
ASP.NET AJAX Web Services
ASP.NET AJAX supports ASMX Web
methods as endpoints for
asynchronous AJAX callbacks
Efficient on the wire (no SOAP or XML)
Efficient on the server (no page lifecycle)
ScriptService attribute on server
indicates Web service is callable from
script
JavaScript proxy on client enables
JavaScript clients to call Web
methods
Script-Callable Web
Service
[System.Web.Script.Services.ScriptService]
public class ZipCodeService : System.Web.Services.WebService
{
[System.Web.Services.WebMethod]
public string[] GetCityAndState (string zip)
{
...
}
}
Declaring a Service
Reference
<asp:ScriptManager ID="ScriptManager1" Runat="server">
<Services>
<asp:ServiceReference Path="ZipCodeService.asmx" />
</Services>
</asp:ScriptManager>
{"zip":"98052"}
Response
HTTP/1.1 200 OK
JSON-encoded Server: ASP.NET Development Server/8.0.0.0
Date: Fri, 29 Dec 2006 21:06:17 GMT
input X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: application/json; charset=utf-8
JSON-encoded Content-Length: 16
Connection: Close
output
["REDMOND","WA"]
ScriptHandlerFactory
Wraps default ASP.NET ASMX handler
<httpHandlers>
<remove verb="*" path="*.asmx" />
<add verb="*" path="*.asmx" validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, ..." />
</httpHandlers>
RestClient-
*.asmx *.asmx/js
ProxyHandler
ScriptHandler- Helper
Factory Classes
*.asmx/methodname RestHandler
WebService-
HandlerFactory Default ASMX handler
JSON
JavaScript Object Notation
Lightweight data interchange format
Easier to read and write than XML
Based on subset of JavaScript
Default interchange format in
ASP.NET AJAX
Microsoft.Web.Script.-
Serialization.JavaScriptSerializer (server)
Sys.Serialization.JavaScriptSerializer
(client)
JSON home page: www.json.org
JSON vs. XML
Point p = new Point(100, 200);
JSON
{"IsEmpty":false,"X":100,"Y":200}
XML
<?xml version="1.0"?>
<Point xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<X>100</X>
<Y>200</Y>
</Point>
The ScriptMethod
Attribute
Optional attribute for script-callable
Web methods
Offers added control over wire format
of calls
Property Description
UseHttpGet True = Use HTTP GET, False = Use HTTP POST (default)
Sys.Services.ProfileService.load(['ScreenName'], onLoadCompleted,
onLoadFailed);
...
function onLoadCompleted(result, context, methodName)
{
window.alert(Sys.Services.ProfileService.properties.ScreenName);
}
Sys.Services.ProfileService.properties.ScreenName = 'Bob';
Sys.Services.ProfileService.save(['ScreenName'], onSaveCompleted,
onSaveFailed);
...
function onSaveCompleted(result, context, methodName)
{
window.alert('Save succeeded');
}