Showing posts with label trouble. Show all posts
Showing posts with label trouble. Show all posts

Saturday, March 24, 2012

Atlas to AJAX conversion problem with delegate. Need help.

I'm having trouble with the sample below since moving from Atlas to AJAX.
I've done a LOT of research with prototypes and delegates, but I can't get
this simple, "class-like" example to work in AJAX as it did in Atlas.
Any help greatly appreciated. Thanks in advance.

============ASPX PAGE=============

<%@dotnet.itags.org. Page Language="vb" AutoEventWireup="false" CodeBehind="test.aspx.vb" Inherits="WebApplication1.test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1">
<Services>
<asp:ServiceReference Path="WS1.asmx" />
</Services>
</asp:ScriptManager>
<input type='button' value='Click me' id='myButton' onclick='doThis(778899)' />
</form>
</body>
</html>

<script type="text/javascript">

function doThis( MyNumber ){

var tmpOjb = new MySampleClass( MyNumber );
tmpOjb._StartMyFunction();
tmpOjb = null;

}
MySampleClass = function( MyNumber )
{
this._MyNumber = MyNumber;

this._StartMyFunction = function()
{
// This WORKS, but with asyncronous call...
// ... this._MyNumber is lost when _MyReturnHandler is run
/* */
WebApplication1.WS1.HelloWorld(
this._MyNumber,
this._MyReturnHandler);
/* */

// This DOES NOT WORK anymore, but DID with Atlas..
// How can I get this to work again??
/*
WebApplication1.WS1.HelloWorld(
this._MyNumber,
{onMethodComplete: Function.createDelegate(this, this._MyReturnHandler)});
*/
}

this._MyReturnHandler = function(result)
{
alert(result + "::::" + this._MyNumber);
}
};

</script>


============ASMX PAGE=============
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Text
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel

<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<System.Web.Script.Services.ScriptService()> _
<ToolboxItem(False)> _
Public Class WS1
Inherits System.Web.Services.WebService

<WebMethod()> _
Public Function HelloWorld(ByVal myNumber As String) As String
Return "Hello World [" & myNumber & "]"


End Function

End Class

The userContext is available for this purpose.

<script type="text/javascript">

function doThis( MyNumber ){

var tmpOjb = new MySampleClass( MyNumber );
tmpOjb.StartMyFunction();
tmpOjb = null;

}

MySampleClass = function(myNumber) {
this._myNumber = myNumber;
}

MySampleClass.prototype = {
StartMyFunction : function() {
var userContext = "Here is some text. This can be a JSON object too";
WebService.HelloWorld(
this._myNumber,
this._OnSucceed,
this._OnFail,
userContext);
},

_OnSucceed : function(result,userContext)
{
alert(result + "::::" +userContext);
},

_OnFail : function(error) {
alert(error);
}
}

</script>

http://ajax.asp.net/docs/ClientReference/Sys.Net/WebRequestClass/WebRequestUserContextMethod.aspx


Thanks Raj! That saved me a lot of time.
I wish I submitted the question earlier.

Thanks again.