I hope you read it if you are unfamiliar with web services,so let us i briefly define what a web service and ScriptManager,
A "web service is the communication platform between two different or same platform applications that allows to use their web method."
What is ScriptManager ?Basically When we use any Ajax control then there is a requirement to use the ScriptManager to handle the Scripting on the client side; without the ScriptManager Ajax controls are not run but also ScriptManager is used to call web service ,to handle errors and on.
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Web Application" (to avoid adding a master page).
- Provide the web site a name such as "CallingWebServiceUsingScriptManager" or another as you wish and specify the location.
- Then right-click on Solution Explorer - "Add New Item" - you see the web service template then click on add
- Right-click on Solution Explorer - "Add New Item" - you see the Web Form template then click on add
- Now drag in drop one ScriptManager,two textBoxes ,one button on Default.aspx Page ,then the source code of the Default.aspx page will be look like as follows
Default.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!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></title>
- </head>
- <body bgcolor="black">
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- <Services>
- <asp:ServiceReference Path="~/WebService.asmx" />
- </Services>
- </asp:ScriptManager>
- <br />
- <h2 style="color: #808000; font-size: medium; font-weight: bolder;">
- Article by vithal wadje</h2>
- <br />
- <br />
- <br />
- <table style="width: 73%; color: White;">
- <tr>
- <td>
- Enter First Number
- </td>
- <td>
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- Enter Second Number
- </td>
- <td>
- <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- </td>
- </tr>
- <tfoot>
- <tr>
- <td>
- </td>
- <td colspan="3">
- <input id="Savebtn" type="button" value="Calculate" onclick="GetDetails();" />
- </td>
- </tr>
- </tfoot>
- </table>
- </form>
- </body>
- </html>
- [WebMethod]
- public int GetAddition(int a, int b)
- {
- //returning sum of given input numbers
- return a + b;
- }
- //To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
- [System.Web.Script.Services.ScriptService]
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- /// <summary>
- /// Summary description for WebService
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
- [System.Web.Script.Services.ScriptService]
- public class WebService : System.Web.Services.WebService
- {
- [WebMethod]
- public int GetAddition(int a, int b)
- {
- //returning sum of given input numbers
- return a + b;
- }
- }

- <asp:ScriptManager ID="ScriptManager1" runat="server">
- <Services>
- <%-- web service reference --%>
- <asp:ServiceReference Path="~/WebService.asmx"/>
- </Services>
- </asp:ScriptManager>
- function GetDetails()
- {
- //reads the values to textboxes
- var a = document.getElementById('TextBox1').value;
- var b = document.getElementById('TextBox2').value;
- // the value to Web method
- WebService.GetAddition(a, b, SuccessCallback, OnfailureCallback);
- }
- OnSucess -> this returns the results after successfully executing the web services method,this parameter is an optional.
- Onfailed -> Due to the some reason web service method failed to execute then its returns the error details,it accepts the error object as a parameter ,this parameter is an optional.
- usercontext -> This is used to provide some additional information,this parameter is an optional.
I hope you understand about the parameter details,now similarly we will create methods for OnSucess and onfailed as.
- //returns output from service
- function SuccessCallback(AddResult)
- {
- //displaying output on alertbox
- alert(AddResult);
- }
- //returns the error after web service failed to execute
- function OnfailureCallback(error)
- {
- //displaying error on alert box
- alert(error);
- }
- <script type="text/javascript">
- function GetDetails()
- {
- //reads the values to textboxes
- var a = document.getElementById('TextBox1').value;
- var b = document.getElementById('TextBox2').value;
- // the value to Web method
- WebService.GetAddition(a, b, SuccessCallback, OnfailureCallback);
- }
- //returns output from service
- function SuccessCallback(AddResult)
- {
- //displaying output on alertbox
- alert(AddResult);
- }
- //returns the error after web service failed to execute
- function OnfailureCallback(error)
- {
- //displaying error on alert box
- alert(error);
- }
- </script>
- <input id="Savebtn" type="button" value="Calculate" onclick="GetDetails();" />
- For detailed code please download the Zip file attached above.


syed ibrahimPosted Sep 18, 2018, 12:39 AM
WebService is not defined error in java script used visual studio 2017
Vithal WadjePosted Apr 25, 2015, 9:51 PM
thanks
Gowtham RajamanickamPosted Apr 25, 2015, 6:30 AM
good
Gowtham RajamanickamPosted Apr 25, 2015, 6:30 AM
good