Get treeview selected node using javascript

Reply

Join Date: Feb 2008
Posts: 52
Reputation: brightline is an unknown quantity at this point 
Solved Threads: 0
brightline brightline is offline Offline
Junior Poster in Training

Get treeview selected node using javascript

 
0
  #1
Feb 24th, 2009
I want to check if the user had selected a node from a treeview or not using javascript.
I'm using VS.NET 2.0.

Thanks In Advance.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 5
Reputation: jaiswarvipin is an unknown quantity at this point 
Solved Threads: 0
jaiswarvipin's Avatar
jaiswarvipin jaiswarvipin is offline Offline
Newbie Poster

Re: Get treeview selected node using javascript

 
0
  #2
Feb 25th, 2009
Hi Dear,


ASP.NET 2.0 has introduced many new promising controls and TreeView is one among them. There has always been a requirement for Tree Control in earlier versions and it was quite hard to manage them with either the third party controls or the lighter version - IE Webcontrols.

Thanks to ASP.NET team, the 2.0 version rolled out with a built-in Treeview Control. The TreeView has many built-in features such as showing a checkbox for all the Tree Nodes. Node level formating, style, etc., Enabling the ShowCheckBoxes="All" property sets it to show a checkbox for all the nodes. The other options are Leaf, None, Parent and Root which show checkboxes at the respective node levels. None doesnt display CheckBoxes.

When we set ShowCheckBoxes="All", we would like to provide a feature where people can select the checkbox on the Root Node so that all the other checkboxes are checked automatically. Basically, when the parent node is checked, all the child nodes should be checked automatically.

It would be intuitive to accomplish this task at the client side without involving a postback.

The following code snippet helps in accomplishing the same.

TreeView Declaration

  1. <asp:TreeView ID="TreeView1" Runat="server" DataSourceID="XmlDataSource1" onclick="client_OnTreeNodeChecked();" ShowCheckBoxes="all">
  2.  
  3. <DataBindings>
  4.  
  5. <asp:TreeNodeBinding DataMember="Category" ValueField="ID" TextField="Name"></asp:TreeNodeBinding>
  6.  
  7. <asp:TreeNodeBinding DataMember="Description" ValueField="Value" TextField="Value"></asp:TreeNodeBinding>
  8.  
  9. </DataBindings>
  10.  
  11. </asp:TreeView>


In the above TreeView declaration Code, you can find the property onclick="client_OnTreeNodeChecked();" event which actually is the JavaScript function which would accomplish this task.

The Javascript Code snippet is as follows:-

  1. <script language="javascript" type="text/javascript">
  2. function client_OnTreeNodeChecked()
  3. {
  4. var obj = window.event.srcElement;
  5. var treeNodeFound = false;
  6. var checkedState;
  7. if (obj.tagName == "INPUT" && obj.type == "checkbox") {
  8. var treeNode = obj;
  9. checkedState = treeNode.checked;
  10. do
  11. {
  12. obj = obj.parentElement;
  13. } while (obj.tagName != "TABLE")
  14. var parentTreeLevel = obj.rows[0].cells.length;
  15. var parentTreeNode = obj.rows[0].cells[0];
  16. var tables = obj.parentElement.getElementsByTagName("TABLE");
  17. var numTables = tables.length
  18. if (numTables >= 1)
  19. {
  20. for (i=0; i < numTables; i++)
  21. {
  22. if (tables[i] == obj)
  23. {
  24. treeNodeFound = true;
  25. i++;
  26. if (i == numTables)
  27. {
  28. return;
  29. }
  30. }
  31. if (treeNodeFound == true)
  32. {
  33. var childTreeLevel = tables[i].rows[0].cells.length;
  34. if (childTreeLevel > parentTreeLevel)
  35. {
  36. var cell = tables[i].rows[0].cells[childTreeLevel - 1];
  37. var inputs = cell.getElementsByTagName("INPUT");
  38. inputs[0].checked = checkedState;
  39. }
  40. else
  41. {
  42. return;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }
  49. </script>
Last edited by peter_budo; Feb 25th, 2009 at 8:46 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 6009 | Replies: 1
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC