Introduction:
This article explains the use of webservices in ASP.NET 3.5 AJAX environment and it tells how to call web service methods from a client-side javascript. It also outlines the new System.Web.Script namespace and explores the ways of defining the client-call to a web service method.
Calling WebService methods in AJAX :
To improve the web user experience, the AJAX functionality of ASP.NET has included in its new version, new ways of calling the web service methods from client-side javascript. Without submitting or rendering a whole page, AJAX enables you to call a server-side method with no post-back.
The client script can make a request to a web method and pass data as input parameters to the method; and data can also be sent back to the client browser from the server.
To enable your application to call ASP.NET Web services by using client script, the server asynchronous communication layer automatically generates JavaScript proxy classes. A proxy class is generated for each Web service for which an <asp:ServiceReference> element is included under the <asp:ScriptManager> control in the page .
<asp:ScriptManager runat="server" ID="scriptManagerId">
<Scripts>
<asp:ScriptReference Path="CallWebServiceMethods.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="WebService.asmx" />
</Services>
</asp:ScriptManager >
<asp:ScriptReference> element is specified to register a javascript file that is to be used in a web page. Only after registering the CallWebServiceMethod.js file, you can call methods on it.
Calling a Web service method from script is asynchronous. To get a return value or to determine when the request has returned, you must provide a succeeded callback function. The callback function is invoked when the request has finished successfully, and it contains the return value (if any) from the Web method call. You can also provide a failed callback function to handle errors. Additionally, you can pass user context information to use in the callback functions.
There are two steps for Consuming web service methods in AJAX . The first step is that there is a slight change in the way you create and define web services. Making calls using client script from a page to the service methods will be the second step.
Creating a web service :
In the System.Web.Scripts.Services namespace, you may find an attribute class “[ScriptSrvice ]” and this need to be applied to a webservice class so that the web service methods can be invoked from the client script.
Similarly, in the same namespace, you may find another attribute class “[ScriptMethod ]” and upon applying this attribute to a web method you can specify which HTTP verb is used to invoke a method and the format of the response.JSON – JavaScript Object Notation is the default serialization format using which data transformation takes place between client-server requests.
Call web service methods using Client Script :
Asp.Net Web Service methods can be called from client scripts asynchronously without a postback and without refreshing the full page. Only data is transferred between the server and client browser.
Write javascript functions to call the web service methods and callback methods. Calls to Web service methods are made by using the proxy class and a list of parameters, name of the succeeded callback function, the failed callback function, and the user context is passed as additional parameters in the call.
Conclusion:
The ASP.NET 3.5 along with the in-built AJAX client-server framework has increased the power of using client scripts to call webservice methods. Most of the settings and javascript code files are loaded automatically when you use Visual Studio 2008. Calling web service methods in the earlier versions of AJAX, ATLAS has few troubles that make the development and debugging little difficult. Now, with ASP.NET 3.5, it has become an inherent part of the much simpler and familiar VS 2008 IDE.
I still have a little more work left to-do before our application is done... :-)
My next step will be to setup the overall UI layout of asp .net ajax application, and provide web services within it.
For More Reference visit following URL :