Ajax| Development Guide
C#
public class index:system.web.ui.page{
private void Page_Load (object sender, EventArgs e) {
Ajax.Utility.RegisterTypeForAjax (typeof (Index));
//...
}
//...
} |
A call to the Registertypeforajax method produces the following JavaScript code on the page (alternatively, you can manually add the following JavaScript code to the page)
The bold part of the code above
NAMESPACE. Pageclass,
AssemblyNameThe meanings are as follows:
NAMESPACE. Pageclass
|
Namespaces and classes for the current page
|
AssemblyName
|
The name of the assembly for the current page
|
The following is a sample output from Sample.aspx in the Ajaxplay project:
<%@ Page inherits= "ajaxplay.sample" codebehind= "Sample.aspx.cs" ...%> ...
|
You can test it by manually opening the SRC path (by viewing the source file and copy) through the browser, and everything works. . If the output of the meaningless text represents so far correct, if the output asp.net error, an error occurred in the middle .
Even if you don't understand how httphandle works, you should be able to understand the description above.
. Through web.config, we can ensure that sending to
ajax/*.ashxThe request is handled by our custom handle, and it is clear that the two script tags are handled by the custom handle
.
writing service-side functions
Now we write
ServerEnd functions, they can be called asynchronously by the client
. Although it is not yet possible to support all return types, we still insist
ServerEnd Add feature
. In the page class of the Codebehind file, add the following method:
' VB.net
_
Public Function Serversideadd (ByVal firstnumber As Integer, ByVal Secondnumber
As Integer) As Integer
return Firstnumber + Secondnumber
End Function |
C#
[Ajax.ajaxmethod ()]
public int Serversideadd (int firstnumber, int secondnumber)
{
return firstnumber + secondnumber;
} |
Note that this function has a ajax.ajaxmethod () custom attribute, and the property service tells the Ajax wrapper class to create a JavaScript proxy for this method so that it can be invoked by the client
.
Custom Client Invocation
Next, invoke the function with JavaScript on the client
. The Ajax wrapper class creates a JavaScript function with two parameters, and the name is the class name. Serversideadd
. As the most basic function, all we need to do is call this method and pass the parameters:
<%@ Page inherits= "ajaxplay.sample" codebehind= "Sample.aspx.cs" ...%>
|
Of course, we can't use such a powerful feature just to alert the viewer through alert, which is why all the client proxies, such as class names. Serversideadd function) with an additional custom attribute at the same time
. This property is used to process
Servercallback function for response:
- 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)