开发者

ASP.Net TreeView Expand/Collapse All does not work when node icons are present

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开发者_如何学Go 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.


This is really from a while back, but if I recall correctly, I simply included a 1px image placeholder in order to not return null on any calls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜