title: treeview control in asp.net very slow to load nodes
body: <p>On my asp.net webpage, the treeview control has thousands of nodes and the tree is very slow to load. The nodes data is coming from sql database using the code below. What can we do to optimize or improve the performance.</p>
  
  <pre><code>public partial class TaxonomyAllTermsTree : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
  
      if (!this.IsPostBack)
      {
          this.AddNodes(this.TreeView1.Nodes, 0, this.LoadData());
          TreeView1.CollapseAll();
      }
  
    }
  
  
    private void AddNodes(TreeNodeCollection nodes, int level, System.Data.DataTable dt)
    {
      string filterExp = string.Format("ParentID='{0}'", level);
      foreach (System.Data.DataRow r in dt.Select(filterExp))
      {
          TreeNode item = new TreeNode()
          {
              Text = r[2].ToString(),
              Value = r[1].ToString(),
              NavigateUrl = "SomeURL.aspx?TermID=" + r[0].ToString()
          };
          this.AddNodes(item.ChildNodes, int.Parse(r[0].ToString()), dt);
          nodes.Add(item);
      }
    }
  
    private System.Data.DataTable LoadData()
    {
      DataTable dt = new DataTable();
      string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ThesaurusConnectionString"].ConnectionString;
  
      using (SqlConnection connection = new SqlConnection(connectionString))
      {
          connection.Open();
          SqlCommand cmd = new SqlCommand("GetFullTree", connection);
          cmd.CommandType = CommandType.StoredProcedure;
  
          SqlDataAdapter da = new SqlDataAdapter(cmd);
  
          da.Fill(dt);
  
          return dt;
      }
    }
  }
  </code></pre>