Using Ajaxmethod can asynchronously invoke the server-side method in the client, simply call the background in JS. cs file in the method, do some JS can not do the operation, such as query the database. Use Ajaxmethod to satisfy a few points:
1. If you don't have a ajax.dll file, download the first one to
2. To add Ajax.dll to a project reference: Right-click the project name-"Add Project Reference" in the VS Solution Explorer-Browse, find Ajax.dll OK
3. Webcofig
If it is IIS7, you need to add <add name= "ajax" verb= "Post,get path=" ajax/* in <system.webServer></system.webServer>. Ashx "Type=" ajax.pagehandlerfactory, Ajax "/>
2: In the CS program plus
protected void Page_Load (OBJECTSENDER,EVENTARGSE)
{
Ajax.Utility.RegisterTypeForAjax (typeof (ClassName));//classname is the class name of this CS file
}
3: Write Cs method, the method preceded by [Ajax.ajaxmethod ()],
If you want to make the Ajaxmethod inside can call session, write: [Ajax.ajaxmethod (Httpsessionstaterequirement.read)],
Such as:
[Ajax.ajaxmethod ()]
The code is as follows |
Copy Code |
public string getaddress (string UserID) { String Address= ""; Do somthing to get this address return address; } |
Note that the method should be written public, otherwise the call in JS will be prompted "Do not support this property or method"
4. Make sure that there are <form runat= "server" ></form> labels on the page.
5. aspx in JavaScript when called with the class name, such as:
var address = classname.getaddress (' 123 '). Value;