javascript:
var Enabled = false;
function GateWay_Enabled(GateWay_Name) {
PageMethods.GateWay_Enabled(GateWay_Name, onRequestComplete, onError);
return Enabled;
}
function onRequestComplete(result) {
Enabled = result;
}
function onError(result) {
alert('Error');
}
var MyVariable = GateWay_Enabled('GateWay_Name');
Server Side Code (C#):
[WebMethod]
[ScriptMethod]
public static bool GateWay_Enabled(string GateWay_Name)
{
bool Enabled = true;
return Enabled;
}
Why is MyVariable always false?
Is there another way to write PageMethods.GateWay_Enabled(GateWay_Name, onRequestComplete, onError); as the return of the GateWay_Enabled function?
I’m looking for something like this :
var MyBoolVariable =
bool.parse(PageMethods.GateWay_Enabled(GateWay_Name,
onRequestComplete, onError));
EDIT 1:
Everything is working and there is no error for PageMethods.
EnablePageMethods in script manager is true.
EDIT 2:
I cannot put MyVariable inside the onRequestComplete() function.
I made MyVariable to make my code easier.
The real code of MyVariable is:
GateWays = [
{ "Cod": 1, "Enabled": GateWay_Enabled('1') },
{ "Cod": 2, "Enabled": GateWay_Enabled('2') },
{ "Cod": 3, "Enabled": GateWay_Enabled('3') },
{ "Cod": 4, "Enabled": GateWay_Enabled('4') },
{ "Cod": 5, "Enabled": GateWay_Enabled('5') },
{ "Cod": 6, "Enabled": GateWay_Enabled('6') },
{ "Cod": 7, "Enabled": GateWay_Enabled('7') }
];
I want to use this array in another place.
I can’t put it in the onRequestComplete() function.
What do I do?
Calling PageMethods is asynchronous: the line
return Enabledis executed before the functiononRequestCompleteis called.Try putting whatever code you have that reads
MyVariableinto theonRequestCompletefunction, usingresultinstead.After your Edit2 and comment, I’d suggest you:
GateWaysarray given an array of input IDs, to avoid making 7 AJAX calls.onRequestCompletemethod.Alternatively if this data doesn’t change between user clicks I’d suggest you get it on the server side.
There’s no easy way to ensure the user-click code waits until all seven of your calls have completed.