With the Gmail service launched by Google, more and more people are paying attention to Ajax technology. The so-called Ajax (Asynchronous JavaScript and XML) technology, it refers to the technology that uses JavaScript and XML to communicate with Web servers without Refreshing Web pages.
Generally, there are two main reasons for using Ajax: fast and cool.
The following example shows how to use Ajax:
1. htmlCode
Btn1 is used to call Ajax code (request the server and fill the returned information in select1 ).
1 < Select ID = "Select1" >Select>
2 <InputID= "Btn1"Value= "Fill select"Type= "Button"Onclick= "Getoptions ();">
2. JavaScript code calls Ajax
1 // Create the request object (the Ajax wrapper)
2 VaR Request = New Request ();
3 // Change this to fit your environment
4 VaR URL = " Http: // localhost/ajax/ " ;
5 Function Getoptions ()
6 {
7 // Call the Ajax
8 // Notice the second parameter is actually a function to handle the response
9 Request. getnocache (URL + " Requests/getoptions. aspx " ,
10 Function (Result)
11 {
12 If (Result. readystate ! = Readystate. Complete)
13 Return ;
14 If (Result. Status = Httpstatus. OK && Result. responsetext ! = "" )
15 {
16 // If the request was successfull and returned data
17 VaR Vals = Result. responsetext. Split ( " ~ " );
18 For (I = 0 ; I < Vals. length; I ++ )
19 {
20 VaR Pair = Vals [I]. Split ( " | " );
21 VaR OP = New Option (pair [ 1 ], Pair [ 0 ], False , False );
22 VaR Sel = Document. getelementbyid ( " Select1 " );
23 Sel. Options [SEL. Length] = OP;
24 }
25 Alert ( " Remember that the new values in Form " +
26 " Element 'select1' are not in viewstate. " +
27 " Code appropriately. " );
28 }
29 Else
30 {
31//Handle the failure condition
32Alert ('get options failed .');
33}
34 }
35 )
36 }
3. aspx File
1 <% @ Page Language = " C # " Codebehind = " Getoptions. aspx. CS " Autoeventwireupo = " False " Inherits = " Ajax. Requests. getoptions " %>
2 <% = Result %>
4. codebehind code
1 Protected string result = string. empty;
2 Private void page_load (Object sender, system. eventargs E)
3 {
4 For (INT I = 0; I < 10 ; I ++)
5 {
6 Result + = I. tostring () + "| Option" + I. tostring () + "~ ";
7 }
8 Result = Result. substring (0, Result. Length-1); // to drop the last '~ '
9 }
More Ajax
Download: Source File Project Test