ASP.NET Treeview花费了太多时间来加载

时间:2015-07-21 15:10:40

标签: c# asp.net treeview

我的页面上有一个treeView,加载时间太长。 如何优化我的代码以减少加载时间?

以下是我的代码示例:

Codebehind:

public static void loadTree(TreeView tree, string username) {

    DataAccessLayer dal = null;
    string sql = null;

    try {
        dal = new DataAccessLayer();
        // SELECT * FROM dbo.TreeView
        sql = "[dbo].[usp_addParentNode]";

        dal.Command = dal.Database.GetSqlStringCommand(sql);
        dal.Command.CommandType = CommandType.StoredProcedure;
        dal.ExecuteDataSet();
        tree.Nodes.Clear();
        foreach(DataRow dataRow in dal.DataSet.Tables[0].Rows) {

            if ((string) dataRow["name"] == username) {

                TreeNode tnParent = new TreeNode();
                tnParent.Text = dataRow["Name"].ToString();
                string value = dataRow["ID"].ToString();

                tnParent.PopulateOnDemand = true;

                tnParent.Expand();
                tree.Nodes.Add(tnParent);

                FillChild(tnParent, value);

            }
        }
    } catch (System.Exception ex) {
        throw ex;
    } finally {
        dal.CloseReader();
    }


}

public static int FillChild(TreeNode parent, string ID) {
    DataAccessLayer dal = null;
    string sql = null;
    try {
        dal = new DataAccessLayer();
        //SELECT * FROM dbo.TreeView where parentId=@parentID
        sql = "[dbo].[usp_addChildNode]";
        dal.Command = dal.Database.GetSqlStringCommand(sql);
        dal.Command.CommandType = CommandType.StoredProcedure;
        dal.Database.AddInParameter(dal.Command, "@parentId", DbType.Int16, ID);
        dal.ExecuteDataSet();
        if (dal.DataSet.Tables[0].Rows.Count > 0) {

            foreach(DataRow dr in dal.DataSet.Tables[0].Rows) {
                TreeNode child = new TreeNode();
                child.Text = dr["Name"].ToString().Trim();
                string temp = dr["ID"].ToString();
                child.Collapse();
                parent.ChildNodes.Add(child);
                //child.ForeColor = Color.Orange;
                FillChild(child, temp);

            }
            return 0;
        } else {
            return 0;
        }
    } catch (System.Exception ex) {
        throw ex;
    } finally {
        dal.CloseReader();
    }

}

treeView表格结构

ID |名称| parentId的

0 个答案:

没有答案