Call a WebService with ASP.Net Ajax
This will be my first post about ASP.Net Ajax.In this post I’m going to show you a very simple example where I will from a Web Form call a Web Service without reloading the page.
When we will use ASP.Net Ajax on our pages we need to add the <atlas:ScriptManager> element. The ScriptManager will automatically add the references to the required JavaScript files that provide Atlas functionality. So it’s required on every page where we will use the ASP.Net Ajax features.
When we want to add a reference to our WebService we use the Service child element of the ScriptManager:
<atlas:ScriptManager runat="server" ID="scriptManager">
<services>
<atlas:servicereference path="~/MyWebService.asmx" />
</services>
</atlas:ScriptManager>
The code above will add a reference to “MyWebService.asmx”.
Let’s take a look at the MyWebService:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyWebService : System.Web.Services.WebService
{
public MyWebService()
{
}
[WebMethod]
public string MyMethod(string value)
{
return value;
}
}
As you can see in the code above, it’s a simple Web Service with the method MyMethod. MyMethod will return the value passed as an argument.
When we have added a reference to our WebService we can simply call it from client-side script by using the name of the WebService as the object:
function CallMyWebService()
{
MyWebService.MyMethod(“My Value”, OnRequestComplete);
}
When we call a WebService we need to specify a client-side method that will be called when our Web Service is done with the execution. The OnRequestComplete (you can change the name if you like) is required because the call to the web Service is asynchronous, and we need to specify a method that should be called on the client-side to notify the client when the execution is done.
function OnRequestComplete(result)
{
alert(result);
}
More than this is not needed to call a WebService with ASP.Net Ajax. Here is the code of the WebForm that will call our WebService:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function CallMyWebService()
{
MyWebService.MyMethod("Value by Sachin", OnRequestComplete);
}
function OnRequestComplete(result)
{
alert(result);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<atlas:ScriptManager ID="scriptManger1" runat="server">
<Services>
<atlas:ServiceReference Path="~/MyWebService.asmx" />
</Services>
</atlas:ScriptManager>
<input type="button" onclick="CallMyWebService();" id="myButton" value="Call MyWebService"/>
</div>
</form>
</body>
</html>
Regards,
Sachin D









