views:

433

answers:

3

Hi all.

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?

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;
                }
            }
        }
    }
A: 

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

  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.

belaz
Browser: IE or Firefox.Regarding the code: I've the following error"treeNodes[element].getElementsByTagName is not a function " ?
Ahmed
Ok sorry, i've changed the code.
belaz
Great, it works now properly. Thanks.
Ahmed
Thanks, and sorry for the time it tooks.
belaz
A: 

IE or Firefox.

Let's also depend on Node 'ToolTip' that is rendered to 'tilte' attribute.

Ahmed
A: 

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

Ahmed