Calling Web Services from Client-Side Code in ASP.Net

In this article we will register our web service in Srcipt-manager Control. In addition we will modify  service method, so it accessible from client. Finally we will write some java script code and display value return from service as output..

Before reading  this article, I strongly recommend to read my previous article on web-services in ASP.. on below link

www.c-sharpcorner.com/Blogs/12612/web-services-in-Asp-Net.aspx.

1. The first thing you need to do is create new Web site in visual studio.

2. Check App_Code folder is there, if not then right click on root directory--> Add ASP.Net Folder-->Click on App_Code...

3. Create folder and name it as Web-services.

4. Right Click on Webservices Folder --->Add New Item--->Select web service and name it as Name.asmx as shown below

AddService2.png

Click add to add the service to the site. This will add .asmx file to your webservices folder and code behind file in App-Code folder

Soln.png

5. Open the Name.cs file from App_Code folder and change the helloworld method as shown below

[WebMethod]

public string HelloWorld(string name)

{

    return "Hello :" + name + "  Welcome to Asp.Net";



6. Save your all changes , right click on Name.asmx in the soln explorer, choose view in browser. you should see your service is running.

webser.png

7. Click on Helloworld link  and type your name and click on invoke button . A new window showing xml that has been return by  web service

WebXml.png

8. Open Name.cs from App-Code folder.

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 Name : System.Web.Services.WebService {

9. Next step is creating page that uses service..So add one page to your web site

Drag and drop Input text and Input Button from toolBox under HTML Control and Scrip-manager from Ajax extensions as shown below

AspPage.png

Note: The Script-Manager control serves as the bridge between the client page and server.It manages the script resource such as JavaScript file and take care of partial page update.


10. Within the script-manager add add <Service> element that in turn contains service-reference for setting path of service as shown below

<asp:ScriptManager ID="ScriptManager1" runat="server">

        <Services>

        <asp:ServiceReference Path="~/WebServices/Name.asmx" />

        </Services>

  </asp:ScriptManager>

11. Below this two line add following javascript code ..
 

<input id="YourName" type="text" />

        <input id="Hello" type="button" value="Welcome" class="style1" />

        <script type="text/javascript">

               function HelloWorldcall(res) {

                alert(res);

            }

            function HelloWorldw() {

                var name = $get('YourName').value;

                Name.HelloWorld(name, HelloWorldcall);

            }

            $addHandler($get('Hello'), 'click', HelloWorldw);

        </script>

12. Save all your changes...run your asp page ...if everything turned out well , you should see message from web service..

OutPut.png

Ebook Download
View all
Learn
View all