Click here to Skip to main content
11,642,613 members (65,771 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Treeview
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:

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

Any help is greatly appreciated.

Thanks,
Posted 7-Aug-12 5:14am
Comments
ryanb31 at 7-Aug-12 12:14pm
   
What is it doing? Is it posting back and running your code?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I check the Parent node it will check all the childnode under that parent node without OnSelectedNodeChanged event.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

use below javascript to check all the child node when you check parent node


<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 >
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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;
  Permalink  
Comments
Deepu S Nair at 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)

  Print Answers RSS
0 DamithSL 254
1 Sergey Alexandrovich Kryukov 204
2 OriginalGriff 160
3 Afzaal Ahmad Zeeshan 129
4 Mika Wendelius 85
0 Mika Wendelius 485
1 DamithSL 457
2 OriginalGriff 365
3 Afzaal Ahmad Zeeshan 258
4 Sergey Alexandrovich Kryukov 254


Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 21 Jan 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100