Skip to main content

How To Use Web Service Behavior in an ASP.NET Page To Invoke the Web service

If you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component "WebService" to access a Web service. The "WebService" behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP).To use the "WebService" behavior, you must attach it to an element using the STYLE attribute. How To Use Web Service Behavior in an ASP.NET Page To Invoke the Web service The first step in using the Web service behavior in a Web page is embedding it using syntax similar to this:
<div id="service" style="BEHAVIOR:url(webservice.htc)"></div>
The above code relies on the behavior functionality built into IE 5 (and higher) to identify the location of the JavaScript file that will be used to call Web services. As discussed previously, the webservice.htc file should be available on the same folder as that of the Web page. It is important to note that the loading of the behavior file occurs on the client rather than on the server.Once embedded, the behavior can be invoked and linked to a WSDL 1.1-compliant Web service using JavaScript code. This is accomplished by referencing the embedded behavior id (service in the previous code) and calling its useService method:
service.useService("http://localhost/TestService/Service.asmx?WSDL","test");
You invoke the useService method in the onload event handler for the page to ensure that the Web service is mapped before any methods are invoked on the Web service. The useService method accepts the following arguments. * Path to the WSDL file for the Web service * A friendly name that can be later used to reference the Web service. This name will be used each time the web methods in the EmployeeService.asmx file are called using the behavior. Service.asmx

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service()
    {


    }
    [WebMethod]
    public string HelloWorld(string strUserName)
    {
        return "Hello " + strUserName;
    }

}
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 id="Head1" runat="server">
    <title>Untitled Page</title>

    <script type="text/javascript">
        function init() {
            service.useService("http://localhost/TestService/Service.asmx?WSDL", "test");

        }
        function tst() {
            iCallID = service.test.callService("HelloWorld", "santosh");

        }
        function onmyresult() {

            service.innerHTML = "Result : " + event.result.value;
        }
    </script>

</head>
<body onload="init();">
    <form id="form1" runat="server">
    <button onclick="javascript:tst()" id="Button1">
        Call Add Web Method</button>
    <div id="service" style="behavior: url(webservice.htc)" onresult="onmyresult();">
    </div>
    </form>
</body>
</html>

Comments

Popular posts from this blog