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.} );}Maybe you can try something like this
KaziManzurRashid:
function GetValue()
{
return PageMethods.GetValueFromServer(function(result)
{
return result;
});
}Maybe you can try something like this
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?
KaziManzurRashid:
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.
KaziManzurRashid:
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();
}
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.
KaziManzurRashid:
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.
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.
Hi,
Thank you for your post!
It seems that you have misunderstanding on AJAX and callback.
The result cann't be obtained like this:
var myValue = GetValue();
It must be obtained like this:
function SetSessionValue(key, value)
{
PageMethods.SetSessionValue(key, value,
OnSucceeded, OnFailed);
}// Callback function invoked on successful
// completion of the page method.
function OnSucceeded(result, userContext, methodName)
{
if (methodName == "GetSessionValue")
{
displayElement.innerHTML = "Current session state value: " +
result;
}
}// Callback function invoked on failure
// of the page method.
function OnFailed(error, userContext, methodName)
{
if(error !== null)
{
displayElement.innerHTML = "An error occurred: " +
error.get_message();
}
}
For more information about callback and webmethod, seehttp://www.asp.net/ajax/documentation/live/tutorials/ExposingWebServicesToAJAXTutorial.aspx
If you have further questions,let me know.
Best Regards,
0 件のコメント:
コメントを投稿