Ajax. NET installation configuration: Download and Installation
First from AJAX.. NET project website, download and decompress the AJAX file, and then follow your preferences in Visual Basic.. NET or C. NET project, and then to AJAX. dll file reference ). The only additional configuration step is to add the following code in the <system. web> element in the web. config file.
- < configuration>
- < system.web>
- < httpHandlers>
- < !-- Register the ajax handler -->
- < add verb="POST,GET" path="ajax/*.ashx"
- type="Ajax.PageHandlerFactory, Ajax" />
- < /httpHandlers>
- ...
- ...
- < /system.web>
- < /configuration>
Install and configure AJAX. NET: Make server-side functions available
To make server-side functions available in JavaScript, two things must be done. First, the function to be used must be marked with Ajax. AjaxMethodAttribute. Second, during page loading events, you must call Ajax. Utility. RegisterTypeForAjax to register classes containing these functions. It seems complicated, but don't worry. In fact, you only need to add two more lines in the code. Let's look at an example.
- // C #
- Public ClassSample: System. Web. UI. Page
- {
- Private VoidPage_Load (ObjectSender, System. EventArgs e)
- {
- // Register the server-side functions that we are interested in.
- // Class
- Ajax. Utility. RegisterTypeForAjax (Typeof(Sample ));
- }
- [Ajax. AjaxMethod ()]
- Public StringGetMessageOfTheDay ()
- {
- Return "Experience is the mother of wisdom";
- }
- }
- 'Vb. NET
- Public ClassSample
- InheritsSystem. Web. UI. Page
- Private SubPage_Load (sender AsObject, eAsEventArgs)
- Handles MyBase. Load
- 'Register the server-side functions that we are interested in
- 'Class
- Ajax. Utility. RegisterTypeForAjax (GetType(Sample ))
- End Sub
- <Ajax. AjaxMethod ()> _
- Public FunctionGetMessageOfTheDay ()As String
- Return "Experience is the mother of wisdom"
- End Function
- End Class
The above example first tells Ajax. NET to find a friendly Ajax method in the Sample class. It is exactly the same class as the actual page, but it can be any. NET class, or multiple classes can be registered. Ajax. NET then browses the specified class to find all the methods marked with AjaxMethodAttribute. The Sample class has a GetMessageOfTheDay.
AJAX. NET installation configuration: use functions and classes in JavaScript
After that, the only thing you have to do is to use it in JavaScript. Ajax. NET automatically creates a JavaScript variable with the same name as the registered class. In this example, the JavaScript variable is Sample. It provides a function with the same name as AjaxMethod, and GetMessageOfTheDay in this example ). As shown below.
- < script language="javascript">
- Sample.GetMessageOfTheDay(GetMessageOfTheDay_CallBack);
- function GetMessageOfTheDay_CallBack(response)
- {
- alert(response.value);
- }
- < /script>
In addition to the JavaScript callback function, JavaScript GetMessageOfTheDay also needs to have the same parameters as the corresponding part of the server. In this case, there is no parameter) to execute and pass the response when the callback is complete. Here, we can see the asynchronous feature of AJAX at work, because the call to GetMessageOfTheDay does not impede the execution of other JavaScript code, nor does it prevent the user from continuing operations on the page. When processing on the server side, Ajax. NET calls the specified callback function GetMessageOfTheDay_CallBack and sends a response composed of the return values on the server side.
The ing between server-side code and JavaScript code may be messy. Figure 1 briefly shows the server-side code and JavaScript code, and the ing between them.
Figure 1: ing between server-side code and JavaScript code
The preceding section briefly introduces the installation and configuration of AJAX. NET.
- Ajax. Net Quick Start
- Download ASP. net ajax Software
- The father of ASP. NET is strongly recommended: ASP. NET AJAX
- Analysis on the AutoComplete control of Asp.net Ajax Control
- Installation and simple configuration of ASP. NET AJAX