Solved

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

Posted on 2007-03-19
10
1,642 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

24 Experts available now in Live!

Get 1:1 Help Now