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;