I have an asp.net treeview (acting as a remote file and folder browser). When a node is selected all child nodes are automatically selected. This works fine (c# code below).
When any child is checked/unchecked I want all related parents to be checked/unchecked as well. I cannot figure this out. I want to use c# to do this.
-item1
------child1
------child2
--child2.1
--child2.2
------child3
Example 1 - if child 2.2 had its checkbox checked then child 2 and item1 will be checked automatically using c# code behind
Example 2 - if item1, child 2 , child 2.1 and child 2.2 were checked and if the user were to uncheck child 2.2 then item1, child 2 would remain checked as child 2.1 is still checked
thanks Damo
below is my code that checks all children of a checked item and works fine.
/// <summary>
/// Checks or unchecks child nodes when a parent node is checked or unchecked.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnTreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
// Determine if checked Node is a root node.
if (e.Node.ChildNodes.Count > 0)
{
// Check or uncheck all of the child nodes based on status of parent node.
if (e.Node.Checked)
ChangeChecked(e.Node, true);
else
ChangeChecked(e.Node, false);
}
}
/// <summary>
/// Recursively checks or unchecks all child nodes for a given TreeNode.
/// </summary>
/// <param name="node">TreeNode to check or uncheck.</param>
/// <param name="check">Desired value of TreeNode.Checked.</param>
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;
}