i have found a piece of code that worked great for me after spending 2h to make complicated algorithlms :
//In Page load
//select where id is null to retrieve the parent nodes
//in a datatable (called table here)
foreach (DataRow row in table.Rows)
{
TreeNode node = new TreeNode();
node.Text = row["title"].ToString();
node.Value = row["id"].ToString();
//you can affect the node.NavigateUrl
node.PopulateOnDemand = true;
TreeView1.Nodes.Add(node);
}
then create the TreeNodePopulate event :
protected void TreeView1_TreeNodePopulate(Object sender, TreeNodeEventArgs e)
{
string id= e.Node.Value;
//do your "select from yourTable where parentId =" + id;
foreach (DataRow row in table.Rows)
{
TreeNode node = new TreeNode(row["title"], row["id"])
node.PopulateOnDemand = true;
e.Node.ChildNodes.Add(node);
}
}
it worked like hell for me, i hope it will help !
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…