Return Array from Web Service using ScriptManager & Javascript

 This is used to call the Web Service using ASP.Net and Script Manager. Must be inserted in .aspx page. 

<asp:ScriptManager runat=”server” ID=”scriptManager”>
<Services>
<asp:ServiceReference path=”WebService/TestService.asmx” />

</Services>

</asp:ScriptManager>

This is the script to call the web service method and print the result on the form:

<script type=”text/javascript”>
        var res1=0;
        var res2=0;
       
        LoadById(2);
       
        function initialize() {
            document.getElementById(“javaControls”).className=””;
            document.write(res1);
            document.write(res2);
        }
       
        function OnSucceed(result) {
            res1=result[0];res2=result[1];
            initialize();
        }
       
        function LoadById(id) {
            TestService.GetResult(id, OnSucceed);
        }
       
        function LoadByDetail(testId) {
            TestService.GetResultByDetail(testId, OnSucceed);
        }
    </script>
 

Next is the Web Service “TestService”:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = “http://tempuri.org/“)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 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 SearchMap : System.Web.Services.WebService
{
   
    [WebMethod]
    public object[] GetResult(string id)
    {
        //get two results from database using id
        object[] results = { row[0], row[1] };
        return results;
    }

    [WebMethod]
    public object[] GetResultByDetail(string testId)
    {
        //get two results from database using test id
        object[] results = { row[0], row[1] };
        return results;
    }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: