A preliminary understanding of Asynchronous JavaScript and XML (AJAX)

Source: Internet
Author: User

Download Ajax. dll http://ajax.schwarz-interactive.de/csharpsample/default.aspx

For the first example, we suggest you refer to its QuickGuide. It feels good.
Here we will reference it again
AJAX. Net Wrapper quick usage guide
Karl Seguin-http://www.openmymind.net/-copyright 2005

(View AjaxGuide.doc for more detailed information)

Step 1-
Create a reference to the ajax. dll file

Step 2-Set up the HttpHandler
In the web. config, set up the HttpHandler, like:

<Configuration>
<System. web>
<HttpHandlers>
<Add verb = "POST, GET" path = "ajax/*. ashx" type = "Ajax. PageHandlerFactory, Ajax"/>
</HttpHandlers>
...
<System. web>
</Configuration>

Step 3-
In the Page_Load event, call the following function:

'Vb. net
Public Class Index
Inherits System. Web. UI. Page

Private Sub Page_Load (sender As Object, e As EventArgs) Handles MyBase. Load
Ajax. Utility. RegisterTypeForAjax (GetType (Index ))
'...
End sub
'...
End Class

// C #
Public class Index: System. Web. UI. Page {
Private void Page_Load (object sender, EventArgs e ){
Ajax. Utility. RegisterTypeForAjax (typeof (Index ));
//...
}
//...
}

 

Step 4-
In the codebehind of your page, add functions (like you normally wowould) that you 'd like to be able to asynchrounsly called by client-side scripting. mark these functions with the Ajax. javascriptMethodAttribute ():

// C #
[Ajax. AjaxMethod ()]
Public int ServerSideAdd (int firstNumber, int secondNumber)
{
Return firstNumber + secondNumber;
}

'Vb. Net
<Ajax. AjaxMethod ()> _
Public Function ServerSideAdd (ByVal firstNumber As Integer, ByVal secondNumber As Integer) As Integer
Return firstNumber + secondNumber
End Function

The wrapper will automatically create a JavaScript function named "ServerSideAdd" which accepts to parameters. When called, this server-side function will be called and the result returned.

Step 5-
Using JavaScript, you can invote the ServerSideAdd function like you wowould any other JavaScript function. you can call it using the two parameters, or optionally pass a call back function. the name of the function is, by default, <name of class>. <name of server side function>, such as Index. serverSideAdd:

AlertIndex. ServerSideAdd (100,99 ));

OR

Index. ServerSideAdd (100,99, ServerSideAdd_CallBack );

Function ServerSideAdd_CallBack (response ){
Alert (response. value );
}

The response exposes three properties, error, value and request.

Note that you can return more complex objects that simple types.

See the demo for additional information:
Http://ajax.schwarz-interactive.de/csharpsample/default.aspx

I imitated it and made the first example. I hope more people will participate in the discussion.

 

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.