Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm using a Treeview with ShowCheckBoxes="All" , so basically I have a tree with all CheckBox Nodes. I'm using the TreeView populate the Tree code behind dynamically.

My requrements are the following:

1) If Select Any Child Node then checked all parent nodes. like lower to upper checked

ex :- Treeview Exmp-
         >>  India 
                   >>New Delhi
                   >>Chandigarh
                   >>Bangalore
                   >>KolKata

if i select Bangalore after automatically checked Chandigarg, New Delhi And parent node India. Jquery,JavaScript,Server Side anything used

Can anyone help me..
Thanks in advance...

What I have tried:

JavaScript
$(document).ready(function () {

            $("#<%= TreeView1.ClientID %>").find("input").click(function () {

                

                    var parentChk = $("#" + $(this).parents(":eq(4)").attr("id").replace('Nodes', 'CheckBox'));
                    debugger;
                    var allChecked = true;
                    var allUnchecked = true;

                    $(this).parents(":eq(4)").find("input").each(function () {

                        if ($(this).attr("checked")) {

                            allUnchecked = false;
                        }
                        else {
                            allChecked = false;
                            parentChk.attr("checked", true);
                        }
                    });

                    if (allChecked) {
                        parentChk.attr("checked", true);
                    }
                    else if (allUnchecked) {
                        parentChk.attr("checked", false);
                    }
            });
        });
Posted
Updated 3-Jan-18 20:56pm
v2

1 solution

Try this its working fine..

On TreeView After_Check Event :

C#
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)    
    {    
       if (e.Node.Checked)    
           CheckAllChildNodes(e.Node, true);    
        else    
          CheckAllChildNodes(e.Node, false);    
    }


Check or Uncheck Nodes Function :

C#
private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)  
    {  
        foreach (TreeNode node in treeNode.Nodes)  
        {  
            node.Checked = nodeChecked;  
            if (node.Nodes.Count > 0)  
            {  
                this.CheckAllChildNodes(node, nodeChecked);  
            }  
        }  
    }
 
Share this answer
 
v3

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