I am trying to create a dynamic tree using C # and asp.net.
I created a lazy load tree using the popdem ondemand attribute.
> <asp:TreeView ID="treeView1" runat="server"
> OnTreeNodePopulate="treeview1_TreeNodePopulate"></asp:TreeView>
For the code, I uploaded my data, but initially I populate the parent nodes. What I want to achieve is when I click on the parent node, then I do the postback, and then populate its child, and then populate its child again, and so on. I have thousands of data, so I donβt want all the data to be populated due to performance. Therefore, I only want to populate the children of the node based on the selected node. See the example below:
>Peter
- - >user1
- - >user2
- - >user3
- - >userPassword
- - >userId
>john
>david
>Jack
- - >user1
- - >user2
- - >userpassword
- - >userId
- - >Permissions
>Laura
- - > admin
- - > permissions
-- > user1
-- > user2
- - >userpassword
- - >userId
- - >Permissions
>...
>...
>...
, . , . , node, node , , , .. , .
#:
private void LoadTreeview()
{
foreach (var dxm in list)
{
TreeNode tnParent = CheckNodeExist(dxm.Node);
if (tnParent== null)
{
TreeNode tn = new TreeNode();
tn.Text = dxm.Node;
tn.Value = dxm.Id.ToString();
tn.SelectAction = TreeNodeSelectAction.None;
tn.Collapse();
treeView1.Nodes.Add(tn);
tn.PopulateOnDemand = true;
tnParent= tn;
}
}
.
TreeNodePopulateEvent: ( node)
protected void treeview1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
ICollection<ITEMS> list = new Collection<ITEMS>();
list = GetData(e.Node.Text);
foreach (var dxm in list)
{
TreeNode tnChild = CheckNodeExist(dxm.Node);
if (tnChild == null)
{
TreeNode tn = new TreeNode();
tn.Text = dxm.Node;
tn.Value = dxm.Id.ToString();
tn.SelectAction = TreeNodeSelectAction.None;
tn.Collapse();
tn.PopulateOnDemand = true;
tnChild = tn;
tnChild.ChildNodes.Add(tnChild);
}
}
}