Recently, Microsoft's tree control was used in the project, so I summarized the usage of some tree controls.
Traverse nodes
Void GetAllNodeText (TreeNodeCollection tnc)
{
Foreach (TreeNode node in tnc)
{
If (node. Nodes. Count! = 0)
GetAllNodeText (node. Nodes );
Response. Write (node. Text + "");
}
}
Achievements
Private void CreateTree (TreeNodeCollection folderTreeNodeCollection, string parentID)
{
If (parentID = "")
{
Dv. RowFilter = "ParentID is null ";
}
Else
{
Dv. RowFilter = "ParentID =" + "'" + parentID + "'";
}
Foreach (DataRowView drv in dv)
{
TreeNode tn = new TreeNode ();
Tn. Expanded = true;
Tn. ID = drv. Row ["TypeID"]. ToString (). Trim ();
Tn. Text = drv. Row ["TypeName"]. ToString (). Trim ();
FolderTreeNodeCollection. Add (tn );
CreateTree (tn. Nodes, tn. ID );
}
}
Traverse selected nodes
Private void getAllCheckedNode (TreeNodeCollection tnc)
{
Foreach (TreeNode tn in tnc)
{
If (tn. Checked = true)
{
Response. Write (tn. Text + "");
}
If (tn. Nodes. Count> 0)
{
GetAllCheckedNode (tn. Nodes );
}
}
}
Select or deselect a node to automatically select or deselect a middle-stage subnode.
Private void CheckedChildNode (TreeNode tn)
{
If (tn. Nodes. Count> 0)
{
Foreach (TreeNode tnn in tn. Nodes)
{
Tnn. Checked = tn. Checked;
CheckedChildNode (tnn );
}
}
}
Call Method
Private void TreeView1_Check (object sender, Microsoft. Web. UI. WebControls. TreeViewClickEventArgs e)
{
SetAllCheckedNode (TreeView1.GetNodeFromIndex (e. Node ));
}