日期:2011-08-21 浏览次数:20485 次
遍历节点
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));
}
1、全部读取并一次性展现
private void Page_Load(object sender, System.EventArgs e)
{
if(IsPostBack)
{
return;
}
DBSystem.ExecuteSQL tmp = new DBSystem.ExecuteSQL("T");
DataTable dt = tmp.GetDataTable("select * from TreeView");//从数据库抓出数据
this.AddNodes(dt,null,"0"); //添加节点
this.TreeView1.SelectedNodeIndex=""; //不选中任何节点。
/*下一节将介绍如何修改TreeView.cs 源码来取消默认选中节点的方法*/