In my ASP.NET AJAX web site, I am attempting to use Page Methods to return data from the web server. I would like to be able to return the value from a page method like so:
function GetValue()
{
return PageMethods.GetValueFromServer();
}
function ManipulateValue()
{
var myValue = GetValue();
// Do something with myValue
}
Unfortunately, calling a page method requires the use of a callback function. I really need to retrieve the value without having to store it in a global variable in the callback function; is there any way to do this?
function GetValue()
{
return PageMethods.GetValueFromServer(
function(result)
{
// The result that is returned from server
//Now do what ever you would like to do.
}
);
}
I tried your code, returning the result in the nested callback function (see above), but when I returned to the code that calls GetValue() the value that ends up getting returned is undefined. :-(
Yes that becasue the ajax call is async. it does not wait for the ajax call to complete,
why do not you call the GetValue in the Callback?
Okay, I'm somewhat confused. I call GetValue(), which calls an asynchronous server method and is passed an in-line Javascript callback function. Are you saying that I should, in the callback function, call GetValue() again? If so, won't that just cause an infinite
loop?
Sorry let me rephrase. You do not need to call the GetValue again in the callback although doing it will not make it recursive. You can call your desired outer ManipulateValue function in the callback but in that case the ManipulateValue will not again call
the GetValue.
Sorry let me rephrase. You do not need to call the GetValue again in the callback although doing it will not make it recursive. You can call your desired outer ManipulateValue function in the callback but in that case the ManipulateValue will not again call
the GetValue.
Okay, I tried that but still no luck. Here is the code I am using; perhaps you can shed some insight on to what I'm doing wrong? It looks like this is, in fact, causing an infinite loop.
Javascript code:
function PrintDateTime()
{
var dateTimeFromServer = RetrieveDateTime();
$get("myLabel").innerHTML = dateTimeFromServer;
}
function RetrieveDateTime();
{
return GetAjaxValue();
}
function GetAjaxValue()
{
return PageMethods.GetCurrentDateTime(
function (result)
{
return RetrieveDateTime();
}
);
}
C# code:
[WebMethod]
public static string GetCurrentDateTime()
{
return DateTime.Now.ToString();
}
function GetAjaxValue()
{
return PageMethods.GetCurrentDateTime(
function (result)
{
$get("myLabel").innerHTML = result;
}
);
}
function showCurrentDateTime()
{
GetAjaxValue();
}
Just call the showCurrentDateTime in your code when you want to show it.
Thanks for the time you've spent addressing my questions, Kazi. Unfortunately, in my code I really need to return the value in the RetrieveDateTime() function. The code in PrintDateTime() is just used for illustration; I need to do more than just put it
in a label. To clarify, here's exactly what I want to do:
var myValue = RetrieveDateTime();
The value is not being displayed on screen but is being used by logic within a JavaScript function. I really just want to be able to call a JavaScipt function and return a value that I can use in the rest of the code.
Hi, Well you have to undertand the nature, it is always. and thats why the best place to assume the value is availabe is the Callback. If you can describe more of your scenerio then I can help.
markrr23
0 Points
5 Posts
Returning results from a page method call
Nov 30, 2007 08:03 PM|LINK
In my ASP.NET AJAX web site, I am attempting to use Page Methods to return data from the web server. I would like to be able to return the value from a page method like so:
function GetValue()
{
return PageMethods.GetValueFromServer();
}
function ManipulateValue()
{
var myValue = GetValue();
// Do something with myValue
}
Unfortunately, calling a page method requires the use of a callback function. I really need to retrieve the value without having to store it in a global variable in the callback function; is there any way to do this?
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: Returning results from a page method call
Nov 30, 2007 08:17 PM|LINK
function GetValue() { return PageMethods.GetValueFromServer( function(result) { // The result that is returned from server //Now do what ever you would like to do. } ); }Maybe you can try something like thisKazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
markrr23
0 Points
5 Posts
Re: Returning results from a page method call
Nov 30, 2007 08:26 PM|LINK
I tried your code, returning the result in the nested callback function (see above), but when I returned to the code that calls GetValue() the value that ends up getting returned is undefined. :-(
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: Returning results from a page method call
Nov 30, 2007 08:39 PM|LINK
Yes that becasue the ajax call is async. it does not wait for the ajax call to complete, why do not you call the GetValue in the Callback?
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
markrr23
0 Points
5 Posts
Re: Returning results from a page method call
Dec 03, 2007 11:48 AM|LINK
Okay, I'm somewhat confused. I call GetValue(), which calls an asynchronous server method and is passed an in-line Javascript callback function. Are you saying that I should, in the callback function, call GetValue() again? If so, won't that just cause an infinite loop?
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: Returning results from a page method call
Dec 03, 2007 12:54 PM|LINK
Sorry let me rephrase. You do not need to call the GetValue again in the callback although doing it will not make it recursive. You can call your desired outer ManipulateValue function in the callback but in that case the ManipulateValue will not again call the GetValue.
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
markrr23
0 Points
5 Posts
Re: Returning results from a page method call
Dec 03, 2007 02:04 PM|LINK
Okay, I tried that but still no luck. Here is the code I am using; perhaps you can shed some insight on to what I'm doing wrong? It looks like this is, in fact, causing an infinite loop.
Javascript code:
function PrintDateTime()
{
var dateTimeFromServer = RetrieveDateTime();
$get("myLabel").innerHTML = dateTimeFromServer;
}
function RetrieveDateTime();
{
return GetAjaxValue();
}
function GetAjaxValue()
{
return PageMethods.GetCurrentDateTime(
function (result)
{
return RetrieveDateTime();
}
);
}
C# code:
[WebMethod]
public static string GetCurrentDateTime()
{
return DateTime.Now.ToString();
}
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: Returning results from a page method call
Dec 03, 2007 03:04 PM|LINK
You can do it in the following way:
function GetAjaxValue() { return PageMethods.GetCurrentDateTime( function (result) { $get("myLabel").innerHTML = result; } ); } function showCurrentDateTime() { GetAjaxValue(); }Just call the showCurrentDateTime in your code when you want to show it.
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
markrr23
0 Points
5 Posts
Re: Returning results from a page method call
Dec 03, 2007 03:40 PM|LINK
Thanks for the time you've spent addressing my questions, Kazi. Unfortunately, in my code I really need to return the value in the RetrieveDateTime() function. The code in PrintDateTime() is just used for illustration; I need to do more than just put it in a label. To clarify, here's exactly what I want to do:
var myValue = RetrieveDateTime();
The value is not being displayed on screen but is being used by logic within a JavaScript function. I really just want to be able to call a JavaScipt function and return a value that I can use in the rest of the code.
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: Returning results from a page method call
Dec 04, 2007 07:47 PM|LINK
Hi, Well you have to undertand the nature, it is always. and thats why the best place to assume the value is availabe is the Callback. If you can describe more of your scenerio then I can help.
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid