943,699 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 24926
  • ASP.NET RSS
Feb 24th, 2009
0

Get treeview selected node using javascript

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
brightline is offline Offline
57 posts
since Feb 2008
Feb 25th, 2009
0

Re: Get treeview selected node using javascript

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

asp.net Syntax (Toggle Plain Text)
  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:-

JavaScript Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jaiswarvipin is offline Offline
5 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: fixing <asp:gridview> header row and selecting rows at client side
Next Thread in ASP.NET Forum Timeline: prevent upload control from uploading a blank file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC