Ajax| Development Guide
private void Page_Load (object sender, EventArgs e) { Utility.registertypeforajax (typeof (User)); } |
At the client we can invoke the GetUser function in this way:
Returns the same value as the Server-SideObject has three attributes (FirstName, LastName and UserId) .
Translator Note: Other types can only be passed by the developer ServerThe End function is implemented by the custom conversion to the type of AJAX support when the value is returned, and Ajax recommends using the ToString method .
Other ways of working
registering functions in other classes
In the above example and the description, we are through the page in the codebehind file to complete the function registration, but not that only in the page codebehide file to complete the registration, we can also register the function in other classes . Recall that the Ajax wrapper class completes the work by looking up a method within a particular class that has the Ajax.ajaxmethod () attribute, which completes the return value description at the client and through two script fragments . Using Ajax.Utility.RegisterTypeForAjax, we can get any details of what we want the class to be . For example, the following example shows how we can use in other classes ServerThe End function is valid:
Public Class Ajaxfunctions _ Public Function Validate (username as String, password as String) as Boolean ' Do something ' Return something End Function End Class |
But we need to register this proxy class first on the call page, and the class name is no longer a page class, it's the class we use:
' VB.net
Private Sub Page_Load (sender as Object, e as EventArgs) Handles MyBase.Load Ajax.Utility.RegisterTypeForAjax (GetType (ajaxfunctions)) '... End Sub |
C#
private void Page_Load (object sender, EventArgs e) { Ajax.Utility.RegisterTypeForAjax (typeof (Ajaxfunctions)); //... } |
Remember, the client invokes a name that uses this format <ClassName>. <ServerSideFunctionName> . Therefore, if the Serversideadd function above is in the Ajaxfunctions class, not the page class, the client call becomes: Ajaxfunctions.serversideadd (1,2)
How does an agent work?
The second script tag, as shown in the following example
<script type= "Text/javascript" src= "/cqyd/ajax/cqyd". Schemesendwatch,cqyd.ashx "> </script>
is generated automatically by the Ajax utility through namespaces, classes, and page assemblies (and can, of course, be added manually), from which we can think of the details of the function that the ajax.pagehandlerfactory is using reflection to get a custom attribute . Obviously, handler looks for functions with Ajaxmethod custom properties, obtains their characteristics (return type, name, parameters) and creates a client proxy based on this information . In particular, Ajax creates a JavaScript object with the same type as the proxy .
return
Unicode
character
Ajax.NET can be from ServerReturns Unicode characters to the client, in order to do this, the value returned when the service-side function returns must be HTML-encoded:
- Ajax: A new way to build Web apps
- Discussion on the error handling mechanism of AJAX (2)
- Discussion on the error handling mechanism of AJAX (1)
- First experience. NET Ajax Brushless New technology
- A brief analysis of Ajax development Technology in Rails system (4)