sending additional parameters to a callback after web method retrieval

Last post 11-21-2008 8:05 AM by Schmakt. 2 replies.

Sort Posts:

  • sending additional parameters to a callback after web method retrieval

    11-20-2008, 11:59 AM
    • Member
      148 point Member
    • Schmakt
    • Member since 12-06-2007, 3:24 PM
    • Charlotte, NC
    • Posts 136

    terrible title, perhaps, but hopefully my question will make sense...

     

    I have an exposed webservice, and everything's groovy.  Javascript making the calls and returning values no problem, but I want to be able to send values to the callback method (is that the right term?) other than just the values returned by the Web Service.  Is this possible?  My example:

     

    WebService

    [WebMethod]

    public string Name(string param1, string param2)

    {

    string strResults =  "*do some stuff*";

    return strResults;

    }

    function fncTesting()

    {

     WebService.WebMethodName("param1","param2",fncGetWebMethodResults);

    }

    fncGetWebMethodResults(x)

    {

     alert(x);

    }

     

    If I do the above, the result is an alert box containing *do some stuff*.

    What I would like to do, instead, if something like this:

    function fncTesting()

    {

     WebService.WebMethodName("param1","param2",fncGetWebMethodResults("Non-WebMethodParam1","Non-WebMethodParam2"));

    }

    fncGetWebMethodResults(x,y,z)

    {

     alert(x);

    alert(y);

    alert(z);

    }

     

    So that fncTesting can send variables to fncGetWebMethodResults without having to return the values of y and z from the WebMethod in an array or something.

    Do this make any sense?  Thank you...

  • Re: sending additional parameters to a callback after web method retrieval

    11-21-2008, 5:15 AM
    Answer
    • Contributor
      2,105 point Contributor
    • aggiekevin
    • Member since 10-21-2006, 3:26 AM
    • Falls Church, VA
    • Posts 418

    Hi,

    This functionality is actually already built-in. The signature of your web service call is

     

    Namespace.ServiceName.Method(parameters, successCallback, failedCallback, userContext)
    

      

    The userContext parameter is a value that you can pass from the calling method to the success and failed callback methods.Your successful callback method will look like this.

     

    function serviceCallback(result, context, methodName) {
    }

      

    where result is the return value of the web service, context is the value passed from the calling method, and methodName is the name of the web service method that was invoked.

     

    Here is a simple example:

     

    1    using System;
    2    using System.Web.Script.Services;
    3    using System.Web.Services;
    4    
    5    namespace KevinBabcock.Examples
    6    {
    7    	[WebService(Namespace = "http://KevinBabcock.org/")]
    8    	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    9    	[ScriptService]
    10   	public class GetTimeService : System.Web.Services.WebService
    11   	{
    12   		[WebMethod]
    13   		public string GetTime()
    14   		{
    15   			return DateTime.Now.ToString();
    16   		}
    17   	}
    18   }
    19   
    
    1    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="KevinBabcock.Examples._Default" %>
    2    
    3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4    
    5    <html xmlns="http://www.w3.org/1999/xhtml" >
    6    <head runat="server">
    7        <title>Example</title>
    8        <script type="text/javascript">
    9        	function pageLoad() {
    10       		var context = "Pass this string to the callback.";
    11       		KevinBabcock.Examples.GetTimeService.GetTime(getTimeSuccessHandler, getTimeFailedHandler, context);
    12       	}
    13   
    14       	function getTimeSuccessHandler(result, context, methodName) {
    15       		$get('spanTime').innerHTML = result;
    16       		$get('spanContext').innerHTML = context;
    17       	}
    18   
    19       	function getTimeFailedHandler(error, context, methodName) {
    20       		alert(error.get_message());
    21       	}    	
    22       </script>
    23   </head>
    24   <body>
    25       <form id="form1" runat="server">
    26   		<asp:ScriptManager ID="ScriptManager1" runat="server">
    27   			<Services>
    28   				<asp:ServiceReference Path="~/GetTimeService.asmx" />
    29   			</Services>	
    30   		</asp:ScriptManager>
    31   		
    32   		<div>		
    33   			Time: <span id="spanTime"></span>
    34   		</div>
    35   		<div>
    36   			Context: <span id="spanContext"></span>
    37   		</div>
    38       </form>
    39   </body>
    40   </html>
    41   
    

      

    I hope this helps.

  • Re: sending additional parameters to a callback after web method retrieval

    11-21-2008, 8:05 AM
    • Member
      148 point Member
    • Schmakt
    • Member since 12-06-2007, 3:24 PM
    • Charlotte, NC
    • Posts 136

     This is EXACTLY what I have been looking for!

    Thank you! :):)

    -jim

Page 1 of 1 (3 items)