How to access the context object as an array in the PageMethods callback

I cannot access the context object using the loop context: var context = [id1, id2, id3];

This callback function works:

function OnChangeSucceeded(result, context, methodName) {
    document.getElementById(context[0]).disabled = result;
    document.getElementById(context[1]).disabled = result;
    document.getElementById(context[2]).disabled = result;
}

      

This callback function doesn't work:

function OnChangeSucceeded(result, context, methodName) {
        for(var indx = 0; indx < context.length; indx++) {
           document.getElementById(context[indx]).disabled = result;
        }

    }

      

0


source to share


3 answers


Thats for a pointer to firebug tvanfosson.

I redid the function and now it works like:

function OnChangeSucceeded(result, context, methodName) {
    for (controlId in context) {
        document.getElementById(context[controlId]).disabled = result;
    }
}

      

I'm not sure if this was because the context was original, created as:



context = [id1, id2, id3];

      

which I have now replaced with:

context = new Array(id1, id2, id3);

      

0


source


It would be nice to see the calling code so we can see how your context is being created. I'm going to assume that you set it up as an association, not an array, so when you use it in the callback, there is no length property (or it is 0).

When you install it, it should look like this:

var context = new Array();
context[0] = 'elem0';
context[1] = 'elem1';
context[2] = 'elem2';

      



not

var context = {0: 'elem0', 1: 'elem1', 2: 'elem2'};

If that is not a problem, try testing it in FireFox / FireBug by setting a breakpoint in the onChangeSucceeded function and examining the actual context object to see what properties it has.

0


source


Is it because of your typo?

for(var index = 0; indx < context.length; indx++) {

      

it should be

for(var indx = 0; indx < context.length; indx++) {

      

0


source







All Articles