Blue Ridge Software LLC
  Encryption
Services
  Webmaster
Services
  Programming   Software   About Us

Program Snippets

  C# - Call Webservice from Javascript (Add To Favorites)
add following to httphandlers of web.config, removing leading spaces after left arrows (auto done in v2005):<br>
------------------------------------------------------------------<br>
< httpHandlers>
	< add verb="*" path="*.asmx" validate="false"<br>
    < remove verb="*" path="*.asmx"/><br>
    < add	verb="*" path="*.asmx" validate="false"<br>
	type="System.Web.Script.Services.ScriptHandlerFactory,<br> System.Web.Extensions,<br>
	Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/><br>
    < add verb="*" path="*_AppService.axd" validate="false"<br>
	type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions,<br>
	Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/><br>
    < add verb="GET,HEAD" path="ScriptResource.axd" validate="false"<br> type="System.Web.Handlers.ScriptResourceHandler,<br> System.Web.Extensions,<br>
	Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/><br>
    < remove verb="*" path="*.asmx"/><br>
    < add verb="*" path="*.asmx" validate="false"<br>
         type="System.Web.Script.Services.ScriptHandlerFactory,<br> 
                       System.Web.Extensions, Version=1.0.61025.0,<br> 
                       Culture=neutral, PublicKeyToken=31bf3856ad364e35"/><br>
  < /httpHandlers><br><br>

add following reference to codebehind of webservice:
------------------------------------------------------------------
// to allow client side calls
using System.Web.Services;

add this method to webservice:
------------------------------------------------------------------
[System.Web.Services.WebMethod]
public static string GetServerResponse(string callerName)
{
if (callerName == string.Empty)
throw new Exception("Web Service Exception: invalid argument");

return string.Format("Service responded to {0} at {1}",
callerName, DateTime.Now.ToString());

}

add scriptmanager to HTML side:
------------------------------------------------------------------
EnablePartialRendering="true" runat="server" />
if already present, insure following attributes are set:
EnablePageMethods="true" EnablePartialRendering="true"

add following functions to the javascript
------------------------------------------------------------------
function OnComplete(arg)
{
alert(arg);
}
function OnTimeOut(arg)
{
alert("timeOut has occured");
}
function OnError(arg)
{
alert("error has occured: " + arg._message);
}

make the javascript call:
------------------------------------------------------------------
PageMethods.GetServerResponse("test", OnComplete, OnError, OnTimeOut);
© 2013- Blue Ridge Software LLC