1.代码如下:
private void Form1_Load(object sender, EventArgs e)
{
treeView1.Nodes.Clear();//清空 树的所有节点
List<T_NavigationModel> Fnods = new T_NavigationBLL().GetModelList("Npid=0");//获取所有根节点
if (Fnods.Count <= 0)//判断要添加的节点个数是否为0,为0则取消
return;
foreach (var a in Fnods)//遍历节点,将节点添加到treeView1控件
{
TreeNode fnode = new TreeNode();
fnode.Text = a.Nname;
treeView1.Nodes.Add(fnode);//将节点添加到treeView1控件
fillchildnods(fnode, a.Nid.ToString());//调用方法,获取 此节点的子节点
}
}
/// <summary>
/// 递归调用方法,获取节点的子节点
/// </summary>
/// <param name="node">节点,将获取的子节点添加到此节点下</param>
/// <param name="pid">父节点id</param>
private void fillchildnods(TreeNode node, string pid)
{
List<T_NavigationModel> Fnods = new T_NavigationBLL().GetModelList("Npid=" + pid);//获取所有子节点
if (Fnods.Count <= 0)//判断要添加的节点个数是否为0,为0则取消
return;
foreach (var a in Fnods)//遍历节点,将节点添加到node节点
{
TreeNode fnode = new TreeNode();
fnode.Text = a.Nname;
node.Nodes.Add(fnode);//将节点添加到node节点
fillchildnods(fnode, a.Nid.ToString());//继续获取子节点
}
}
ASP.NET用递归法绑定Treeview控件