Ajax implemented in ASP.net (2)

Source: Internet
Author: User
Tags object end functions http request net string tostring wrapper
Ajax|asp.net












Of course, we do not want to use this powerful ability to warn users only . This is why all client proxies, such as JavaScript sample.serversidead functions, also accept other features . This feature is the callback function invoked to handle the response:

Sample.serversideadd (100,99, serversideadd_callback);

function Serversideadd_callback (response) {

if (response.error!= null) {

alert (response.error);

Return

}

alert (Response.value);

}

We can see from the above code that we have specified another parameter . Serversideadd_callback (see also the above code) is a client function for handling server responses . This callback function receives a response object that exposes three primary properties

value--the value that the server -side function actually returns (whether it is a string, a custom object, or a dataset).

error--error messages, if any .

Request--xml the original response of the HTTP request .

context--the context object .

First we check the error only to see if there is an error . by throwing an exception in a server -side function, you can easily handle the error attribute . in this simplified example, the user is then warned with this value . the request attribute can be used to obtain more information .
Processing Type

Return Complex Type

The Ajax wrapper can handle not only the integers returned by the Serversideadd function . It also currently supports basic types such as integers, strings, double, Booleans, DateTime, datasets, and DataTables, as well as custom classes and Arrays . all other types return their ToString values .

The returned datasets is almost the same as the real. NET DataSet . assuming a server -side function returns the dataset, we can display the contents of the client in the following code:

Ajax can also return custom classes, and the only requirement is that they must be marked with the Serializable attribute . The following classes are assumed:

[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");

}

}

We can register the GetUser agent by calling Registertypeforajax:

private void Page_Load (object sender, EventArgs e) {

Utility.registertypeforajax (typeof (User));

}

This allows GetUser to be invoked asynchronously at the client:

The value returned in the response is actually an object that exposes the same properties as the server-side objects (FirstName, LastName, and UserID).
Custom Converters


We've seen that Ajax. NET wrapper can handle many different. NET types. But except for a lot. NET class and built-in type, the wrapper invokes only ToString () on other types that cannot be returned correctly. To avoid this situation, Ajax. NET wrapper allows the developer to create an object converter for smooth delivery of complex objects between the server and the client.

Other matters

Registering functions in other classes

In the example above, our server-side functions are placed in the code behind the execution page. However, there is no reason why these functions should not be placed in a separate class file. Remember that the wrapper works by discovering all the methods with Ajax.ajaxmethod in the specified class. The required classes are specified by a second script tag. Using Ajax.Utility.RegisterTypeForAjax, we can specify any class that is required. For example, it is reasonable to use our server-side functions as separate classes:

Public Class Ajaxfunctions

_

Public Function Validate (username as String, password as String) as Boolean

' Do something

' Return something

End Function

End Class

You can have the Ajax wrapper create an agent by specifying the type of the class instead of the page:

private void Page_Load (object sender, EventArgs e) {

Ajax.Utility.RegisterTypeForAjax (typeof (Ajaxfunctions));

//...

}

Remember, the name of the client proxy is . . Therefore, if the Serversideadd function is placed in the fictitious Ajaxfunctions class above, the client invocation should be: Ajaxfunctions.serversideadd (1,2).

How the agency works in the end

The second script tag generated by the Ajax tool (also inserted manually) passes the page's namespace, class name, and assembly. Based on this information, Ajax.pagehandlerfactory is able to use reflection to get the details of any function that has a particular attribute. Obviously, the handler functions look for functions that have ajaxmethod properties and get their signatures (return type, name, and parameters) from the ability to create the necessary client proxies. Specifically, the wrapper creates a JavaScript object with the same name as the class that provides the proxy. In other words, given a server-side class ajaxfunctions with an Ajax Serversideadd method, we get the Ajaxfunction JavaScript object that exposes the Serversideadd function. This action is seen if you point the browser to the path of the second script label.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.