| | |
Get treeview selected node using javascript
Please support our ASP.NET advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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
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:-
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)
<asp:TreeView ID="TreeView1" Runat="server" DataSourceID="XmlDataSource1" onclick="client_OnTreeNodeChecked();" ShowCheckBoxes="all"> <DataBindings> <asp:TreeNodeBinding DataMember="Category" ValueField="ID" TextField="Name"></asp:TreeNodeBinding> <asp:TreeNodeBinding DataMember="Description" ValueField="Value" TextField="Value"></asp:TreeNodeBinding> </DataBindings> </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)
<script language="javascript" type="text/javascript"> function client_OnTreeNodeChecked() { var obj = window.event.srcElement; var treeNodeFound = false; var checkedState; if (obj.tagName == "INPUT" && obj.type == "checkbox") { var treeNode = obj; checkedState = treeNode.checked; do { obj = obj.parentElement; } while (obj.tagName != "TABLE") var parentTreeLevel = obj.rows[0].cells.length; var parentTreeNode = obj.rows[0].cells[0]; var tables = obj.parentElement.getElementsByTagName("TABLE"); var numTables = tables.length if (numTables >= 1) { for (i=0; i < numTables; i++) { if (tables[i] == obj) { treeNodeFound = true; i++; if (i == numTables) { return; } } if (treeNodeFound == true) { var childTreeLevel = tables[i].rows[0].cells.length; if (childTreeLevel > parentTreeLevel) { var cell = tables[i].rows[0].cells[childTreeLevel - 1]; var inputs = cell.getElementsByTagName("INPUT"); inputs[0].checked = checkedState; } else { return; } } } } } } </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.
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: fixing <asp:gridview> header row and selecting rows at client side
- Next Thread: prevent upload control from uploading a blank file
Views: 6009 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 activexcontrol advice ajax application asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn chat checkbox child class click compatible confirmationcodegeneration content contenttype countryselector courier css database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdown dropdownmenu edit feedback flash flv folder form forms google grid gridview homeedition hosting identity iframe iis index javascript jquery list menu mono mssql multistepregistration nameisnotdeclared object objects order problem profile ratings refer rotatepage save search security serializesmo.table session silverlight smartcard software sql suse textbox tracking typeof unauthorized update validation vb vb.net video view virtualdirectory vista visual-studio visualstudio vs2008 web webarchitecture webdevelopemnt wizard xml





