如何在TreeView中获取TreeNode的ClientID?

时间:2021-10-31 23:38:37

How to get ClientID of a TreeNode in a TreeView based on one of its rendered attributes, for example, its title attribute (In my case it's unique) ,using either Server-Side or Client-Side code?

如何使用服务器端或客户端代码基于其呈现的属性之一获取TreeView中TreeNode的ClientID,例如,其标题属性(在我的情况下它是唯一的)?

I go with this code, but it doesn't work, any suggestion?

我使用此代码,但它不起作用,任何建议?

    // Retrieves TreeNode ClientID.
    function GetTreeNodeID(nodeTitle)
    {                            
        var treeNodes = document.getElementById('tvMenu').childNodes;
        var treeLinks;

        for(var i=0 ; i<treeNodes.length ; i++)
        {                                                
            treeLinks = treeNodes[i].getElementsByTagName('a');                        
            for(var j=0 ; j<treeLinks.length ; j++)
            {                                        
                if(nodeTitle == treeLinks[j].title && treeLinks[j].title != "");
                {                        
                    alert("Par: " + nodeTitle);
                    alert("Title: " + treeLinks[j].title);
                    return treeLinks[j].id;
                }
            }
        }
    }

The above code that is mentioned with the question always returns the id of root node, any suggestion?

问题中提到的上述代码总是返回根节点的id,有什么建议吗?

1 个解决方案

#1


innerText or innerHtml or textContent ? Wich browser do you use ?

innerText或innerHtml或textContent?您使用哪种浏览器?

  function GetTreeNodeID(nodeInnerText)
        {                            
            var tree = document.getElementById('tvMenu');
            var treeLinks =  tree.getElementsByTagName('A');    

            for(var element in treeLinks )
            {                                             

                if((nodeInnerText == treeLinks[element].innerText) && (treeLinks[element].innerText != ""))
                    {                        
                        alert("Par: " + nodeInnerText);
                        alert("innerText: " + treeLinks[element].title);
                        return treeLinks[element].id;
                    }

            }
        }

Look here for a sample code.

在这里查看示例代码。

#1


innerText or innerHtml or textContent ? Wich browser do you use ?

innerText或innerHtml或textContent?您使用哪种浏览器?

  function GetTreeNodeID(nodeInnerText)
        {                            
            var tree = document.getElementById('tvMenu');
            var treeLinks =  tree.getElementsByTagName('A');    

            for(var element in treeLinks )
            {                                             

                if((nodeInnerText == treeLinks[element].innerText) && (treeLinks[element].innerText != ""))
                    {                        
                        alert("Par: " + nodeInnerText);
                        alert("innerText: " + treeLinks[element].title);
                        return treeLinks[element].id;
                    }

            }
        }

Look here for a sample code.

在这里查看示例代码。