How to get the treeview node count using javascript?
-
I have a treeview in my form. I need to get the nodes count of the treeview using javascript. I know it is possible in code behind. But I want to do it in javascript. I used the following code,but getting an error that "controlid.Nodes.length" is null or not an object.
var controlid = document.getElementById('ctl00_MainContent_CreateWizard_UnitsTreeView');
var nodecount = controlid.Nodes.length;
alert(nodecount);Any idea how to get the count of nodes? Please help. Thanks,
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
-
I have a treeview in my form. I need to get the nodes count of the treeview using javascript. I know it is possible in code behind. But I want to do it in javascript. I used the following code,but getting an error that "controlid.Nodes.length" is null or not an object.
var controlid = document.getElementById('ctl00_MainContent_CreateWizard_UnitsTreeView');
var nodecount = controlid.Nodes.length;
alert(nodecount);Any idea how to get the count of nodes? Please help. Thanks,
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
Why do you require this using javascript. treeview renders nested tables. So you need to use javascript to count tables. :) why dont you count in the server side and store in an hidden field ? :doh:
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Microsoft Bing MAP using Javascript
CLR objects in SQL Server 2005
Uncommon C# Keywords/xml> -
I have a treeview in my form. I need to get the nodes count of the treeview using javascript. I know it is possible in code behind. But I want to do it in javascript. I used the following code,but getting an error that "controlid.Nodes.length" is null or not an object.
var controlid = document.getElementById('ctl00_MainContent_CreateWizard_UnitsTreeView');
var nodecount = controlid.Nodes.length;
alert(nodecount);Any idea how to get the count of nodes? Please help. Thanks,
Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.
The treeview is rendered as a table, so there is no easy way to get a count of nodes - the nodes are actually table rows and cells. You could apply a css class or other attribute to the nodes in the code behind when you are creating your treeview - then use javascript to count the number of elements with that class - very simple if you use JQuery. Are you adding or removing nodes dynamically with javascript? If not then you could just count them as they are added server-side and embed them in your javascript when the page is rendered. This[^] post will show you how to embed asp.net variable. Hope that helps.