Implement Ajax in ASP. NET (4)

Source: Internet
Author: User
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.

Other items

Register functions in other classes

In the above example, all our server functions are placed behind the execution pageCode. However, there is no reason not to place these functions in a separate class file. Remember, the way the wrapper works is to find all methods with Ajax. ajaxmethod in the specified class. The required class is specified through the Second Script tag. With Ajax. Utility. registertypeforajax, we can specify any classes required. For example, it is reasonable to use our server-side functions as separate classes:

Public class ajaxfunctions

<Ajax. ajaxmethod ()> _

Public Function validate (username as string, password as string) as Boolean

'Do something

'Return something

End Function

End Class

By specifying the type of the class rather than the page, you can let the Ajax package create a proxy:

Private void page_load (Object sender, eventargs e ){

Ajax. Utility. registertypeforajax (typeof (ajaxfunctions ));

//...

}

Remember that the name of the client proxy is <classname>. <serversidefunctionname>. Therefore, if the serversideadd function is placed in the fictitious ajaxfunctions class above, the client call should be: ajaxfunctions. serversideadd (1, 2 ).

How does proxy work?

The Second Script tag generated by the Ajax Tool (which can also be inserted manually) passes the page namespace, class name, andProgramSet. Based on this information, Ajax. pagehandlerfactory can use reflection to obtain detailed information about any function with specific attributes. Obviously, the processing function searches for functions with the ajaxmethod attribute and obtains their signatures (return types, names, and parameters), from the ability to create necessary client proxies. Specifically, the Package creates a JavaScript Object with the same name as the class, which provides a proxy. In other words, given a server-side class ajaxfunctions with Ajax serversideadd method, we will get the ajaxfunction JavaScript Object that exposes the serversideadd function. If you direct the browser to the path of the Second Script tag, you will see this action.

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;

}

Http://www.shpan.com/Detail.asp? Id = 399

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.