title: Dynamically create a treeview
body: <p>I am trying to create a treeview dynamically using c# and asp.net.</p>
  
  <p>I have created a lazy load treeview using the populate ondemand attribute. </p>
  
  <pre><code>&gt;  &lt;asp:TreeView ID="treeView1"  runat="server" 
  &gt;              OnTreeNodePopulate="treeview1_TreeNodePopulate"&gt;&lt;/asp:TreeView&gt;
  </code></pre>
  
  <p>Behind code I have loaded my data but initially I populate the parent nodes. What I want to achieve is when i click on parent node I then do a postback and then populate its child and then again populate its child's and so now. I have thousands of data so i dont want all data to be populated due to performance. So thats the reason why I only want to populate the node childs based on selected node. See example below:</p>
  
  <pre><code>&gt;Peter
      - - &gt;user1
      - - &gt;user2
      - - &gt;user3
         - - &gt;userPassword
         - - &gt;userId
  &gt;john
  &gt;david
  &gt;Jack
      - - &gt;user1
      - - &gt;user2
         - - &gt;userpassword
         - - &gt;userId
         - - &gt;Permissions
  &gt;Laura 
      - - &gt; admin
      - - &gt; permissions
         -- &gt; user1
         -- &gt; user2
           - - &gt;userpassword
               - - &gt;userId
               - - &gt;Permissions           
  &gt;...
  &gt;...
  &gt;...
  </code></pre>
  
  <p>As you can see there can be multiple parent nodes and multiple layers. These will be populated dynically based on what i pass in to DB. Everytime i click on node it will expand the node and populate its child using postback and then when you click on its child again it will do a postback and populate its child again etc. So i wanted help on how to create a dynamic treeview.</p>
  
  <p>c# :</p>
  
  <pre><code>private void LoadTreeview()
  {
   //Load data
   // Get data from DB.
   //loop through the list and build its parent nodes.
    foreach (var dxm in list)
    {
                  TreeNode tnParent = CheckNodeExist(dxm.Node); //I check to see if exists.
                  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; //lazy load
                      tnParent= tn;
                  }
  
  }
  </code></pre>
  
  <p>This method above is called on page load.</p>
  
  <p>On TreeNodePopulateEvent: (when a node is clicked on)</p>
  
  <pre><code>protected void treeview1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
          {
              ICollection&lt;ITEMS&gt; list = new Collection&lt;ITEMS&gt;();           
  
              list = GetData(e.Node.Text); //pass in the node you have selected  this will go and check in DB if the node does have any child nodes. If so will return with child nodes.
  
              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);                  
  
                  }
              }
          }
  </code></pre>