在我的 asp.net 网页上,treeview 控件有数千个节点,并且树的加载速度非常慢。节点数据来自使用以下代码的 sql 数据库。我们可以做些什么来优化或提高性能。
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;
}
}
}