简体   繁体   中英

Dictating Save Order of Controls in VB.Net

I have a parent page that contains a placeholder. Span elements that contain IFrames are added to this placeholder as new IFrames are needed. Each IFrame contains an asp control. The parent has this code, fired onClick of the parent page:

function saveAll() {

        for (i = 0; i < frames.length; i++) {
            for (j = 0; j < frames[i].length; j++) {
                if(frames[i][j] != null && frames[i][j] != ''){document.getElementById(frames[i][j].toString()).contentWindow.Save();}
            }
        }
    }

which calls a save function within each control, which in turn, fires a button click, which fires the server side save function for that specific control. To someone trying to read this in 30 seconds...the previous sentence might be found to be confusing...so hopefully this might clarify the big picture here:

在此处输入图片说明

The problem I'm having is that the server is processing the controls out of order, despite the frames being ordered.

the frames object in the aforementioned javascript is a 2D array organized like this:

frames
[[controlA_instanceA,controlA_instanceB,controlA_instanceC],
[controlB_instanceA,controlB_instanceB],
[controlC_instanceA,controlC_instanceB,etc],
[etc]]

The loop, the firing of the child controls, and the frames array is all working correctly (hence why I didn't show the code here). However, could someone point me in the right direction on how to enforce the order the server processes the controls in?

Your controls need a sequence number assigned to them. Then the first step of your save all would be to decompose the 2D array into a 1D array organized by the sequence number.

When you are adding an item to the frames array you need to add them as an object with two members:

var seqNo = 0; // at some point early in the script

...

frames[x][y] = {frame: (iFrame), sequence: seqNo}; //When the Control is added
seqNo++;

where (iFrame) is the id of the frame.

Then your save all function would look like this:

function saveAll() {

    var iFrames = [];

    for (i = 0; i < frames.length; i++) 
    {
        for (j = 0; j < frames[i].length; j++) {
            if(frames[i][j] != null && frames[i][j] != '')
            {
                iFrames.push(frames[i][j]);
            }
        }
    }

    iFrames.sort(function(a,b){ return a.sequence - b.sequence});

    for ( t = 0; t < iFrames.length; t++)
    {
        document.getElementById(iFrames[t].frame.toString()).contentWindow.Save();
    }
}

In this way you are ensuring that the order is preserved because you sorted the array by the sequence number.

This is to say that the problem could be that the array appears to be sorted but it is not and and not assuming the issue is the order in which the server is responding to the click events.

EDIT:

If the server is responding to the events out of order then you may need a different approach.

If this is an ajax site and you are posting back via a web method then I would suggest creating a new web method that takes an array as a parameter.

Push all the content you wish to save into the array and then pass it back to the server for processing in a specific order.

If this is a classic forms site then you can handle a custom postback in your page load event.

Either way it sounds like you need to re-evaluate the way this is processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM