Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  


.NET 4.0: Call WebService Asynchronously using JavaScript
You can create PageMethods on your aspx pages which can be called asynchronously using JavaScript. However, if you do the same thing on multiple pages, you would have to duplicate these methods.

The better solution is to create a shared method in a WebService (.asmx).
You can call it using JavaScript.

1. Create a WebService file in your web application. Ensure you uncomment the header, so that ScriptService is enabled:

/// <summary>
/// Summary description for MyWebService
/// </summary>
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
public class MyWebService : System.Web.Services.WebService

public void DoAction(int id, int otherId)
// logic here

2. On your page, you must have a scriptManager. Either reference the service there, or add it programmatically in code.

<asp:ScriptManager runat="server" ID="smMain" >
<asp:ServiceReference Path="~/WebServices/MyWebService.asmx" />

3. You can now call the method using various javascript methods. For more information, see:

Ensure that you use the full namespace and class name to reference the method:

function SendRequest()
MyNamespace.MyWebService.DoAction(1, 2);

Created By: amos 9/25/2013 5:06:55 PM
Updated: 9/25/2013 5:07:19 PM