How to Use the Microsoft ajax Library (ajax. ajaxMethod)

Source: Internet
Author: User

Four things need to be done with ajax. ajaxMethod

1: add <add verb = "POST, GET" path = "ajax/*. ashx" type = "Ajax. PageHandlerFactory, Ajax"/> contacts in webCofig

2: add

Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Ajax. Utility. RegisterTypeForAjax (typeof (NewsDrop); // NewsDrop is the class name of this CS File
}

3: Add [Ajax. AjaxMethod ()] before the method.

[Ajax. AjaxMethod (Ajax. HttpSessionStateRequirement. Read)]

4: add the class name when calling ASPX JAVASCRIPT

The AjaxMethod can be used to asynchronously call the server method on the client. Simply put, the method in the background. cs file is called in JS to perform operations that cannot be done by JS, such as querying the database. The following requirements must be met when using AjaxMethod:

1. If no ajax. dll file exists, download one first.

2. Add ajax. dll to project reference: In Solution Explorer of VS, right-click the project name, click "add project reference", and Click Browse. Find ajax. dll to confirm.

3. <HttpHandlers> and <add verb = "POST, GET" path = "ajax/*. ashx" type = "Ajax. PageHandlerFactory, Ajax"/> nodes in webCofig

For IIS7. webServer> </system. add <add name = "ajax" verb = "POST, GET" path = "ajax/* To webServer> /*. ashx "type =" Ajax. pageHandlerFactory, Ajax "/>

2: add

Copy codeThe Code is as follows:
Protected void Page_Load (objectsender, EventArgse)
{
Ajax. Utility. RegisterTypeForAjax (typeof (ClassName); // ClassName is the class name of this CS File
}

3: Compile the CS method. Add [Ajax. AjaxMethod ()] before the method.

To enable the AjaxMethod to call the session, write it as [Ajax. AjaxMethod (HttpSessionStateRequirement. Read)],

For example:

Copy codeThe Code is as follows:
[Ajax. AjaxMethod ()]
Public string GetAddress (string UserID)
{
String Address = "";
// Do somthing to get the address here
Return Address;
}

Note that the method should be written as public. Otherwise, the system will prompt "This attribute or method is not supported" when calling in JS"

4. Make sure the page contains the <form runat = "server"> </form> label.

5. Add the class name when calling aspx javascript, for example:

Var address = ClassName. GetAddress ('20140901'). value;

Related Article

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.