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

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
OnTreeNodeCheckChanged
event. But when I click the parent nodes it won't work. Here is my code behind:

C#
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);
                   else
                       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:
XML
<asp:TreeView ID="trSubInd" Runat="server" Width="159px" Height="177px" ShowCheckBoxes="All" OnSelectedNodeChanged="trSubInd_CheckChanged">
                </asp:TreeView>


Any help is greatly appreciated.

Thanks,
Posted
Comments
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


XML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
    $(function () {
        $("[id*=TreeViewForMasters] input[type=checkbox]").bind("click", function () {
            var table = $(this).closest("table");
            if (table.next().length > 0 && table.next()[0].tagName == "DIV") {
                //Is Parent CheckBox
                var childDiv = table.next();
                var isChecked = $(this).is(":checked");
                $("input[type=checkbox]", childDiv).each(function () {
                    if (isChecked) {
                        $(this).attr("checked", "checked");
                    } else {
                        $(this).removeAttr("checked");
                    }
                });
            } 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");
                //}
            }
        });
    })

</script>
 
Share this answer
 
SQL
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
 
Comments
Deepu S Nair 21-Jan-15 7:01am    
Answering old questions adds nothing to the previous solution and is likely to attract
downvoting.

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