最近做的项目用到了微软的树控件,所以我总结了部分树控件的用法。
遍历节点
void GetAllNodeText(TreeNodeCollection tnc)
{
foreach(TreeNode node in tnc)
{
if(node.Nodes.Count!=0)
GetAllNodeText(node.Nodes);
Response.Write(node.Text + " ");
}
}
建树
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);
}
}
遍历选中节点
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);
}
}
}
选中或取消选中节点 , 自动选中或取消选中期子节点
private void CheckedChildNode(TreeNode tn)
{
if (tn.Nodes.Count>0)
{
foreach (TreeNode tnn in tn.Nodes)
{
tnn.Checked=tn.Checked;
CheckedChildNode(tnn);
}
}
}
private void TreeView1_Check( object sender, Microsoft.Web.UI.WebControls.TreeViewClickEventArgs e)
{
setAllCheckedNode(TreeView1.GetNodeFromIndex(e.Node));
}