Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:

I am trying to create a code where if I click the Parent node it will check all the childnode under that parent node. I used the
event. But when I click the parent nodes it won't work. Here is my code behind:

protected void trSubInd_CheckChanged(Object sender, TreeNodeEventArgs e)
           //foreach (TreeNode node in trSubInd.CheckedNodes)//loops on the checked nodes
               if (e.Node.ChildNodes.Count > 0)//if parent node
                   if (e.Node.Checked)
                       ChangeChecked(e.Node, true);
                       ChangeChecked(e.Node, false);

       private void ChangeChecked(TreeNode node, bool check)
           // "Queue" up child nodes to be checked or unchecked.
           if (node.ChildNodes.Count > 0)
               for (int i = 0; i < node.ChildNodes.Count; i++)
                   ChangeChecked(node.ChildNodes[i], check);

           node.Checked = check;

here is my aspx code:
<asp:TreeView ID="trSubInd" Runat="server" Width="159px" Height="177px" ShowCheckBoxes="All" OnSelectedNodeChanged="trSubInd_CheckChanged">

Any help is greatly appreciated.

ZurdoDev 7-Aug-12 12:14pm    
What is it doing? Is it posting back and running your code?

I check the Parent node it will check all the childnode under that parent node without OnSelectedNodeChanged event.
Share this answer
use below javascript to check all the child node when you check parent node

<script type="text/javascript" src=""></script>
    <script type="text/javascript">
    $(function () {
        $("[id*=TreeViewForMasters] input[type=checkbox]").bind("click", function () {
            var table = $(this).closest("table");
            if ( > 0 &&[0].tagName == "DIV") {
                //Is Parent CheckBox
                var childDiv =;
                var isChecked = $(this).is(":checked");
                $("input[type=checkbox]", childDiv).each(function () {
                    if (isChecked) {
                        $(this).attr("checked", "checked");
                    } else {
            } else {
                //Is Child CheckBox
                var parentDIV = $(this).closest("DIV");
                if ($("input[type=checkbox]", parentDIV).length == $("input[type=checkbox]:checked", parentDIV).length) {
                    $("input[type=checkbox]", parentDIV.prev()).attr("checked", "checked");
                //else {
                //    $("input[type=checkbox]", parentDIV.prev()).removeAttr("checked");

Share this answer
TreeView.SelectedNode.Checked = true;
To check all the child-nodes you can do the following:

 foreach (TreeNode node in TreeView.SelectedNode.ChildNodes)
    node.Checked = true;
Share this answer
Deepu S Nair 21-Jan-15 7:01am    
Answering old questions adds nothing to the previous solution and is likely to attract

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900