|
|
|
Get treeview selected node using javascript
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
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
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 ajax alltypeofvideos anathor appliances application asp asp.net bc30451 beginner box browser button c# cac checkbox commonfunctions control dataaccesslayer database datagridview datagridviewcheckbox datalist deployment development dgv dialog dropdownlist dynamic dynamically edit embeddingactivexcontrol expose feedback fileuploader fill findcontrol flash form formatdecimal formview google gridview gudi iis image javascript list listbox login microsoft mobile mouse mssql news novell numerical opera panelmasterpagebuttoncontrols parent project radio redirect registration relationaldatabases reportemail richtextbox save schoolproject search security select sessionvariables silverlight smoobjects software sql-server sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video videos vista visualstudio web webapplications webdevelopemnt webdevelopment webprogramming webservice xsl youareanotmemberofthedebuggerusers





