我有一个asp.net treeview(充当远程文件和文件夹浏览器)。选择节点后,将自动选择所有子节点。这很好用(下面是c#代码)。
当检查/取消选中任何一个孩子时,我也希望检查/取消选中所有相关父母。我无法弄清楚这一点。我想用c#来做这件事。
-item1
------ child1
------的child2
--child2.1
--child2.2
------ child3
示例1 - 如果子2.2已选中其复选框,则将使用后面的c#代码自动检查子项2和项目1
示例2 - 如果检查了item1,child2,child 2.1和child 2.2,并且如果用户取消选中子2.2然后item1,则子2将保持被检查,因为子2.1仍然被检查
感谢 达莫
下面是我的代码,它检查已检查项目的所有子项并且工作正常。
/// <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;
}
答案 0 :(得分:0)
我想出来了
我有两个功能如下。第一个检查所有孩子,第二个检查所有父母。 希望这有助于某人。 达莫
ChangeCheckedChildren(e.Node, e.Node.Checked);
ChangeCheckedParents(e.Node, e.Node.Checked);
/// <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 ChangeCheckedChildren(TreeNode node, bool check)
{
try
{
// "Queue" up child nodes to be checked or unchecked.
if (node.ChildNodes.Count > 0)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
ChangeCheckedChildren(node.ChildNodes[i], check);
}
node.Checked = check;
}
catch (Exception ex)
{
}
}
/// <summary>
/// Recursively checks or unchecks all parent 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 ChangeCheckedParents(TreeNode node, bool check)
{
try
{
if (node.Parent == null) // if we are at the root node
{
if (node.ChildNodes.Count > 0)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Checked == true)
{
node.Checked = true;
return;
}
}
node.Checked = false;
}
else
{
node.Checked = check;
}
}
else
{
// Check all parents if the user is checking
if (check == true)
{
node.Checked = check;
ChangeCheckedParents(node.Parent, check);
}
else
{
// Do not uncheck a parent if any of its other children or their children are checked
if (node.ChildNodes.Count > 0)
{
// Default to not check and check if required
node.Checked = false;
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].Checked == true)
{
node.Checked = true;
}
}
ChangeCheckedParents(node.Parent, check);
}
else
{
node.Checked = check;
ChangeCheckedParents(node.Parent, check);
}
}
}
}
catch (Exception ex)
{
}
}