0. How does it work?
1. Create Server Functions
2. Client call
3. Callback Function
4. Processing type
5. Session Access
Appendix
0. How does it work?
With the Ajax package, ASP. NET developers can quickly and conveniently deploy pages that are easy to use Ajax functions.
Ajax relies on the broker to assign and process requests to and from the server. The purpose of the wrapper is to hide the implementation of the XMLHTTPRequest object. NET functions are marked as Ajax methods. After marking, Ajax creates the corresponding JavaScript functions. These functions (like any JavaScript function) can be called on the client using XMLHttpRequest as a proxy. These proxies are then mapped back to the server-side functions.
Suppose there is a. net function:
Ublic int add (INT firstnumber, int secondnumber) {return firstnumber + secondnumber ;}
The package automatically creates a JavaScript function named "add" with two parameters. When you call this function using JavaScript (on the client), the request is sent to the server and the result is returned to the client.
1. Create Server Functions
(1) reference the Ajax. dll file in the project.
(2) Compile FunctionsCode
Namespace forajax {public class example {[Ajax. ajaxmethod ()] public static int serversideadd (INT firstnumber, int secondnumber) {return firstnumber + secondnumber ;}}}
The function contains the [Ajax. ajaxmethod ()] attribute set. This property tells the wrapper how to create these methodsJavascriptProxy to be called on the client.
2. Client call
(1) Reference Ajax. dll and server project files on the website.
(2) create httphandler in Web. config
<Configuration> <system. web>
Note: deploy on iis7.
<System. webserver>
(3) Client page call
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. CS "inherits =" _ default "%> <HTML>
The "ajax/forajax. example, forajax. ashx" format is "ajax/namespace. pageclass, assemblyname. ashx. You can manually navigate to the SRC path in the browser (ViewSource code, Copy and paste the path) to check whether everything is normal.
Through web. config, we have ensured that all ajax/*. ashx requests are processed by the custom Ajax wrapper. The packer is responsible for creating the JavaScript function sample. serversideadd with two parameters for foreground calls.
3. Callback Function
Sample. serversideadd (100,99, serversideadd_callback); function serversideadd_callback (response) {If (response. Error! = NULL) {alert (response. Error); return;} alert (response. Value );}
The response object response discloses three main properties.
- Value -- the value actually returned by the server-side function (whether it is a string, a custom object, or a dataset ).
- Error -- error message, if any.
- Request -- XML: the original response of the HTTP request.
- Context -- Context object.
4. Processing type
The Ajax Wrapper can not only process integers returned by the serversideadd function. It currently supports integers, strings, double, booleans, datetime, datasets, and datatables, as well as basic types such as custom classes and arrays. All other types return their tostring values.
(1) returned Datasets
Similar to the real. Net dataset. Suppose a server-side function returns dataset. We can use the following code to display the content on the client:
<Script language = "JavaScript"> // asynchronous call to the mythical "getdataset" server-side function getdataset () {ajaxfunctions. getdataset (getdataset_callback);} function getdataset_callback (response) {var DS = response. value; If (Ds! = NULL & typeof (DS) = "object" & Ds. tables! = NULL) {var S = new array (); s [S. length] = "<Table border = 1>"; for (VAR I = 0; I <Ds. tables [0]. rows. length; I ++) {s [S. length] = "<tr>"; s [S. length] = "<TD>" + Ds. tables [0]. rows [I]. firstname + "</TD>"; s [S. length] = "<TD>" + Ds. tables [0]. rows [I]. birthday + "</TD>"; s [S. length] = "</tr>";} s [S. length] = "</table>"; tabledisplay. innerhtml = S. join ("");} else {alert ("error. [1, 3001] "+ response. request. responsetext) ;}}</SCRIPT>
(2) return custom class
AJAX can also return custom classes. The only requirement is that the [serializable ()] attribute must be used. Suppose there are the following classes:
[Serializable ()] public class user {private int _ userid; private string _ firstname; private string _ lastname; Public int userid {get {return _ userid ;}} public String firstname {get {return _ firstname;} Public String lastname {get {return _ lastname;} public user (INT _ userid, string _ firstname, string _ lastname) {This. _ userid = _ userid; this. _ firstname = _ firstname; this. _ lastname = _ lastname;} public user () {} [ajaxmethod ()] public static user getuser (INT userid) {// replace this with a DB hit or something return new user (userid, "Michael", "Schwarz ");}}
You can register the getuser agent by calling registertypeforajax:
Private void page_load (Object sender, eventargs e) {utility. registertypeforajax (typeof (User ));}
In this way, you can call getuser asynchronously on the client:
<Script language = "JavaScript"> function getuser (userid) {user. getuser (getuser_callback);} function getuser_callback (response) {If (response! = NULL & response. value! = NULL) {var user = response. value; If (typeof (User) = "object") {alert (user. firstname + "" + User. lastname) ;}}getuser (1); </SCRIPT>
The value returned in the response is actually an object that exposes the same attributes (firstname, lastname, and userid) as the server object ).
(3,Returns Unicode characters.
The Ajax. Net package can return Unicode characters from the server to the client. Therefore, data must be HTML encoded on the server before being returned. For example:
[Ajax. ajaxmethod] Public String test1 (string name, string email, string comment) {string html = ""; HTML + = "hello" + name + "<br> "; HTML + = "Thank you for your comment <B>"; HTML + = system. web. httputility. htmlencode (comment); HTML + = "</B>. "; return HTML ;}
(4) custom Converter
We have seen that the Ajax. Net package can process many different. Net types. However, except for a large number of. Net classes and built-in types, the wrapper only calls tostring () for other types that cannot be correctly returned (). To avoid this, the Ajax. Net package allows developers to create an object converter for smoothly passing complex objects between the server and the client.
5. Session Access
[Ajax. ajaxmethod (Ajax. httpsessionstaterequirement. read)] public void documentreleased () {If (system. web. httpcontext. current. session ["documentswaiting"] = NULL ){//...}}
Appendix:
Ajax. DLL for Ajax development under ASP. NET
ASP. NET Condiments: Ajax