Unable to call web service from ScriptManager

Last post 07-04-2007 3:44 PM by awj. 1 replies.

Sort Posts:

  • Unable to call web service from ScriptManager

    07-04-2007, 1:21 PM
    • Member
      116 point Member
    • awj
    • Member since 07-29-2005, 1:34 PM
    • Posts 65

     I asked this question a couple of weeks ago and a couple of people who posted replies didn't follow up so I'm still seeking a solution.

    I have a very simple web service that I want to make a callback to (from a ScriptManager). At this stage all it returns is the out-of-the-box Hello World string.

    Here is the full code for the web service and the page which I'm trying to call it from. I use a Master Page but I can't see that there's anything on the Master Page which would upset the workings of what I'm trying to achieve so I'll leave it for now (unless you specifically ask to see it - it contains only layout/design markup).

    The web service:

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

    namespace andrewWebsite
    {
         [
    WebService(Namespace = http://tempuri.org/)]
         [
    WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
         [System.Web.Script.Services.
    ScriptService()]
         public class DataService : System.Web.Services.WebService
         {
              [
    WebMethod]
              public string HelloWorld()
              {
                   return "Hello World";
              }
         }
    }

    And the page itself:

    <%@ Page Language="C#" MasterPageFile="~/andrewWebsite.Master" Title="Web Service Test" CodeBehind="test.aspx.cs" Inherits="andrewWebsite.test" %>

    <asp:Content ID="main" ContentPlaceHolderID="mainContent" runat="server">

    <script type="text/javascript">
    <!--
         function useService()
         {
              ret = DataService.HelloWorld(OnComplete, OnTimeOut, OnError);
         }
         function OnComplete(retResult)
         {
              alert(retResult);
         }
         function OnTimeOut(retResult)
         {
              alert(
    'Timeout');
         }
         function OnError(retResult)
         {
              alert(
    'Error');
         }
    // -->
    </script>

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
         <Services>
              <asp:ServiceReference Path="DataService.asmx" />
         </Services>
    </
    asp:ScriptManager>

    <input id="Button1" type="button" value="button" onclick="useService()" />

    </asp:Content>

    The code-behind file is empty except for the default stuff.

    So when I click Button1 I'm expecting to see a message box saying "Hello World". Can you shed any light as to why this isn't occurring?

     

  • Re: Unable to call web service from ScriptManager

    07-04-2007, 3:44 PM
    Answer
    • Member
      116 point Member
    • awj
    • Member since 07-29-2005, 1:34 PM
    • Posts 65

    I found the answer to this one: the web service method in the calling (javascript) function needed the namespace.

    So in the code (see above) I now have

    <script type="text/javascript">
    <!--
         function useService()
         {
              ret = andrewWebsite.DataService.HelloWorld(OnComplete, OnTimeOut, OnError);
         }

    I'm not entirely sure why this is - I've seen a few examples/tutorials on how to achieve this and they haven't qualified the method with the namespace. Also, everything's in the same namespace here, so why should it matter?

    Anyway, if anyone else gets stuck like I did, give it a go. 

Page 1 of 1 (2 items)