Using Ajaxmethod, you can asynchronously invoke the server-side method on the client, simply by invoking the method in the background. cs file in JS.
Do some things that JS can't do, such as querying the database
Use Ajaxmethod to meet a few points:
1. If you don't have a ajax.dll file, download one to
2. Add Ajax.dll to the project reference: right-click the project name in the VS Solution Explorer--"Add Project reference
"--Browse, find Ajax.dll OK
3. Webcofig
Type= "ajax.pagehandlerfactory, Ajax"/> Node
If it is IIS7, you need to add <add to the Handlers node in the <system.webServer></system.webServer>
Name= "Ajax" verb= "Post,get" path= "Ajax/*.ashx" type= "Ajax.pagehandlerfactory, Ajax"/>
2: Add in CS program
protected void Page_Load (OBJECTSENDER,EVENTARGSE)
{
Ajax.Utility.RegisterTypeForAjax (typeof (ClassName));//classname is the class name of this CS file
}
3: Write the CS method, the method is preceded by [Ajax.ajaxmethod ()],
If you want to make Ajaxmethod inside can call session, write: [Ajax.ajaxmethod
(Httpsessionstaterequirement.read)],
Such as:
Copy Code
The 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, or it will be called in JS to "do not support this property or method"
4. Make sure the page has <form runat= "server" ></form> tags.
5. Added class name when called in aspx JavaScript
Original source: http://www.ynpxrz.com/n710705c2023.aspx
How to use ASP. Ajaxmethod