views:

874

answers:

0

I've used the following js code to expand/collapse all nodes of an ASP.Net TreeView control:

// handle tree - this doesn't seem to work when the tree contains node images
function TreeviewExpandCollapseAll(treeViewId, expandAll) {
    var displayState = (expandAll == true ? "none" : "block");
    var treeView = document.getElementById(treeViewId);
    if (treeView) {
        var treeLinks = treeView.getElementsByTagName("a");
        var nodeCount = treeLinks.length;
        alert(nodeCount);

        for (i = 0; i < nodeCount; i++) {
            if (treeLinks[i].firstChild.tagName) {
                if (treeLinks[i].firstChild.tagName.toLowerCase() == "img") {
                    var currentToggleLink = treeLinks[i];
                    var childContainer = GetParentByTagName("table", currentToggleLink).nextSibling;
                    if (childContainer.style.display == displayState) {
                        eval(currentToggleLink.href);
                    }
                }
            }
        } //for loop ends
    }
}

//utility function to get the container of an element by tagname
function GetParentByTagName(parentTagName, childElementObj) {
    var parent = childElementObj.parentNode;
    while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
        parent = parent.parentNode;
    }
    return parent;
}

The problem is that when I have NodeStyle-ImageUrl="img/FolderSmall.gif" , it the expanding fails after opening the first child node - ONLY when there are node images present.

The JS error is childContainer is null:

                    if (childContainer.style.display == displayState) {
                        eval(currentToggleLink.href);

I'm sure someone else has run into this, but all the code I've found for expand/collapse fails at the same point.