Solved

Need help checking and unchecking child and parent nodes in treeview..using c#

Posted on 2007-03-19
10
1,674 Views
Last Modified: 2011-04-14
I am trying to firgure out a method to check all child nodes if a parent node is checked...BUT uncheck all parent nodes if a child node is unchecked without changing the siblings or other child nodes.

Just to make sure this is making sense..

If I check a node any child node of that node is autmoatically checked.

HOwever if I uncheck a node...sibling and child nodes are NOT unchecked..but all parent nodes are.

I am guessing the basics of the method would look like this..but of course I cant figure out the code to check and uncheck child and parent nodes.

Anyone able to help me out with the code?...has to be c#

protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
        if (e != null)
        {
            if (e.Node.Checked == true)
            {

            }
            else
            {

            }
        }
        else
        {
        }
    }
0
Comment
Question by:Prysson
  • 5
  • 5
10 Comments
 
LVL 3

Expert Comment

by:Kate12
ID: 18753721
Just create a recursion method to check all nodes within the node path that was changed:
protected void TreeView1_TreeNodeCheckChanged(object sender, TreeViewEventArgs e)
{
    if (e != null)
    {
        this.ChangeState(e.Node, e.Node.Checked);
    }
}

private void ChangeState(TreeNode node, bool shouldCheck)
{
    node.Checked = shouldCheck;
    foreach (TreeNode child in node.nodes)
    {
        child.Checked = shouldCheck;
        if (child.Nodes.Count > 0)
        {
            this.Changed(child, shouldCheck);
        }
    }
HTH
0
 

Author Comment

by:Prysson
ID: 18755471
Kate
Your code doesnt compile.

The ChangedState method line

foreach (TreeNode child in node.nodes)

the .nodes has a does not contain definition error


the same error with the Nodes in

 if (child.Nodes.Count > 0)

and finally the same error with

this.Changed(child, shouldCheck);

also I dont think your example gets me what I am looking for. Your code appears to uncheck child noes if the parent node is uncheck but that is not what I want to have occur. I want child nodes to be checked if the parent is checked...but I want the parent nodes to be unchecked if a child node is unchecked.


0
 
LVL 3

Accepted Solution

by:
Kate12 earned 500 total points
ID: 18755521
   protected void TreeView1_TreeNodeCheckChanged(object sender, TreeViewEventArgs e)
        {
            if (e != null)
            {
                if (e.Node.Checked)
                {
                    this.CheckAllChildNodes(e.Node);
                }
                else
                {
                    this.UncheckFromNodeToRoot(e.Node);
                }
            }
        }

        private void UncheckFromNodeToRoot(TreeNode treeNode)
        {
            treeNode.Checked = false;
            if (treeNode.Parent != null)
            {
                this.UncheckFromNodeToRoot(treeNode.Parent);
            }
        }

        private void CheckAllChildNodes(TreeNode node)
        {
            node.Checked = true;
            foreach (TreeNode child in node.Nodes)
            {
                child.Checked = true;
                if (child.Nodes.Count > 0)
                {
                    this.UncheckChildNodes(child);
                }
            }
        }

HTH
0
 

Author Comment

by:Prysson
ID: 18755670
Kate12...

Your code seems to be close but there are stil some compile errors where I cant figure out what you are trying to do..

Specifically in the CheckAllChildNodes method.

twice you use node.Nodes however that give an error

'System.Web.UI.WebControls.TreeNode' does not contain a definition for 'Nodes'

Also you reference  an UncheckChildNodes(child) but UncheckChildNodes isnt a method. I am assuming you mean to call CheckAllChildNodes but cant be sure.

btw thanks for your help.
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755718
Yes i meant to do the recursionthere so it is not UncheckChildNodes but CheckAllChildNodes instead. Also i misunderstood that you are running this in ASP.NET and not Windows Forms. so instead of using te "Nodes" property you have to replace it by "ChildNodes" properties.

HTH
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Prysson
ID: 18755734
One other things..am I using the correct event handler for recursivley checking child nodes?

I assumed that the compile errors were meant to be node.ChildNodes and that you meant to call the method itself with the CheckAllChildNodes..if I make those changed it compiles..but of course nothing actually happens..he he...I put in debugging stops to check the code..and it never runs teh code..so I am wondering is I have the right event handler chosen or not.

0
 

Author Comment

by:Prysson
ID: 18755761
Kate I am going to accept you solution...the code looks like it will do exactly what I want I just seem to be having some wiring issue or something with the event handler.

I thank you for the help.

Does the TheeNodeCheckChanged seem to you like the appropriate event handler for running this code?
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755797
As far as i can tell TreeNodeCheckChanged is the right handler for doing what you need.
Let me know how it goes.
0
 
LVL 3

Expert Comment

by:Kate12
ID: 18755887
A quick fix on firring the event. Just by checking the tree it will not cause to postback so you will have to update the tree between post-backs or you can force the post-back, here is an article showing how to do it:
http://www.developerfusion.co.uk/forums/thread/121579/?Ajax_CallBack=true
0
 

Author Comment

by:Prysson
ID: 18756760
Kate12 If you woudl like to go post this at my other question on this very issue over here

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22460342.html

That way I can give you the points since this is basically the solution I need for that question.

Thanks for looking into this for me.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why Does This Page Not Show Up? 4 22
Resolve Dependency Issues 4 40
Remove greater than sign 3 41
Not showing JavaScript in the list 5 32
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now