Click here to Skip to main content
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 6: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 > 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>
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 562
1 Sergey Alexandrovich Kryukov 484
2 Maciej Los 325
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 4 Jul 2014
Copyright © CodeProject, 1999-2014
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