求教treeview的遍历,在树节点被选中后其所有子节点也被选中。大家帮我!!
不在click事件中书写代码,因为需要在其他地方调用。
private void treeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
getcheck(treeView1.Nodes);
}
private void getcheck(TreeNodeCollection tc)
{
foreach(TreeNode TNode in tc )
{
if(TNode.Nodes.Count != 0)
{
TNode.Checked = treeView1.SelectedNode.Checked;
}
getcheck(TNode.Nodes);
}
}
这个程序会陷入死循环,请问我该怎么写呢?
------解决方案--------------------private List <int> GetAllNode(TreeNode node)
{
foreach (TreeNode n in node.Nodes)
{
int i = Convert.ToInt32(n.Tag);
nodes.Add(i);
GetAllNode(n);
}
return nodes;
}
------解决方案--------------------用JS 实现效果会更好:
<asp:TreeView ID= "TreeView1 " runat= "server " onclick= "return OnTreeNodeChecked(event); " BackColor= "#C0C0FF " ShowLines= "True " Width= "213px " ShowCheckBoxes= "All ">
<Nodes>
<asp:TreeNode Text= "aaa " Value= "aaa ">
<asp:TreeNode Text= "a1 " Value= "a1 " > </asp:TreeNode>
<asp:TreeNode Text= "a2 " Value= "a2 " > </asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text= "bbb " Value= "bbb " >
<asp:TreeNode Text= "b1 " Value= "b1 " > </asp:TreeNode>
<asp:TreeNode Text= "b2 " Value= "b2 " >
<asp:TreeNode Text= "b3 " Value= "b3 "> </asp:TreeNode>
<asp:TreeNode Text= "b4 " Value= "b4 "> </asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
<HoverNodeStyle BackColor= "#8080FF " Font-Underline= "True " />
<SelectedNodeStyle Font-Underline= "True " />
<NodeStyle Font-Underline= "True " />
</asp:TreeView>
------解决方案--------------------续...
function OnTreeNodeChecked(e)
{
//debugger;
var ele = event.srcElement;
//debugger;
// ele.style.backgroundColor = "#6699ff ";;
ele.style.readOnly = true;
ele.setAttribute( "Title ", 'abc ');
if(ele.type== 'checkbox ')
{
var childrenDivID = ele.id.replace( 'CheckBox ', 'Nodes ');
var div = document.getElementById(childrenDivID);
if(div != null)
{
var checkBoxs = div.getElementsByTagName( 'INPUT ');
for(var i=0;i <checkBoxs.length;i++)
{
if(checkBoxs[i].type== 'checkbox ')
checkBo