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("></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 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:
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 = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
    public Service()

    public string HelloWorld(string strUserName)
        return "Hello " + strUserName;

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<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;

<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(" onresult="onmyresult();">

No comments:

Post a Comment