Question

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?

Was it helpful?

Solution

Calling PageMethods is asynchronous: the line return Enabled is executed before the function onRequestComplete is called.

Try putting whatever code you have that reads MyVariable into the onRequestComplete function, using result instead.

After your Edit2 and comment, I'd suggest you:

  1. Refactor your PageMethod to return the whole GateWays array given an array of input IDs, to avoid making 7 AJAX calls.
  2. Make all this AJAX be called when the user clicks the link, and then put the subsequent code in the onRequestComplete method.

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.

OTHER TIPS

You need to re factor your code. now you are doing something after GateWay_Enabled method with result. put that stuff in separate method and call it from onRequestComplete method;

var Enabled = false;
function GateWay_Enabled(GateWay_Name) {
    PageMethods.GateWay_Enabled(GateWay_Name, onRequestComplete, onError);
}

function onRequestComplete(result) {
    alert(result); // you will get results here; 
    Enabled = result;
    //do something with value 
}
function onError(result) {
    alert('Error');
}

GateWay_Enabled('GateWay_Name');  // you can't get direct output from this method,  
// have to get results from success callback method or onError callback method 

Did you add EnablePageMethods="true" in your ScriptManager?

you are checking the value of MyVariable before onRequestComplete has executed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top