日期:2014-05-20  浏览次数:20464 次

VS2005 treeview 动态增加它的深度
我以前写treeview   都是规定几级就写几层循环,但是现在我现在数据库中添加域删

除数据就可以控制treeview的层次
  急用

------解决方案--------------------
不明白你的意思,你现在需要什么功能?
------解决方案--------------------
递归方法实现.
------解决方案--------------------
递归
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
TreeNode newNode;
newNode = new TreeNode();
newNode.Value = "0 ";
newNode.Text = "【团购网】 ";
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Expand;
TreeView1.Nodes.Add(newNode);
TreeView1.ShowLines = true;
}
}
protected void TreeView1_TreeNodePopulate1(object sender, TreeNodeEventArgs e)
{
TreeNode newNode;
if (e.Node == null)
{
newNode = new TreeNode();
newNode.Value = "0 ";
newNode.Text = "开始站点 ";
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Expand;
TreeView1.Nodes.Add(newNode);

}
else
{
string sql = "select * from tb_class Where parentid= " + e.Node.Value + " and isClass=0 ";
SqlConnection cn = DB.CreateConn();
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
newNode = new TreeNode();
newNode.Text = dr[ "classname "].ToString();
newNode.Value = dr[ "classid "].ToString();
newNode.NavigateUrl = "addProd.aspx?classid= " + dr[ "classid "].ToString();
//设置PopulateOnDemand为true,确保下面的子节点仍然可以动态生成。
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Expand;
e.Node.ChildNodes.Add(newNode);
}
dr.Close();
dr.Dispose();
cmd.Dispose();
cn.Close();
cn.Dispose();
cn = null;
}
}