由于数据量太大,加载树时间很长,所以进行了优化
前台 .aspx
<asp:Panel ID="Panel2" runat="server" Height="600px" ScrollBars="Auto"> <asp:TreeView ID="TreeView1" runat="server" ForeColor="Black" OnTreeNodeExpanded="TreeView1_TreeNodeExpanded" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"> <LeafNodeStyle HorizontalPadding="3px" /> <ParentNodeStyle HorizontalPadding="3px" /> <RootNodeStyle HorizontalPadding="3px" /> <SelectedNodeStyle ForeColor="#009933" /> </asp:TreeView> </asp:Panel>
后台.cs
#region wsw更改树的加载方式 private void LoadNewTree() { DataTable dtp = pagBLL.GetList(" superfuncid=0 order by sort asc").Tables[0]; if (dtp.Rows.Count > 0) { //首先清除树型控件内的结点 this.TreeView1.Nodes.Clear(); for (int i = 0; i < dtp.Rows.Count; i++) { //实例出一个结点 TreeNode Node = new TreeNode(); Node.Text = dtp.Rows[i]["funcname"].ToString(); Node.Value = dtp.Rows[i]["pagefuncid"].ToString(); Node.ToolTip = dtp.Rows[i]["funcurl"].ToString(); Node.ImageUrl = "../../" + dtp.Rows[i]["imageurl"].ToString(); this.TreeView1.Nodes.Add(Node);//增加父节点,这时直接往树TreeView1上加 this.LoadAddSign(Node.ChildNodes,Convert.ToInt32(dtp.Rows[i]["pagefuncid"].ToString()));//wsw更改 TreeView1.CollapseAll(); } } } /// <summary> /// 用于加载显示 树形控件 +号的,加载下面的一个节点。 /// </summary> /// <param name="Conn"></param> /// <param name="tn"></param> /// <param name="DeptId"></param> private void LoadAddSign( TreeNodeCollection tn, int DeptId) //wsw更改 { DataTable table = pagBLL.GetList(" superfuncid=" + DeptId + " order by sort asc").Tables[0]; if (table.Rows.Count > 0) { //加载"一个"1级节点,就是为了显示 + 号 tn.Add(new TreeNode()); } } /// <summary> /// 点击 + 号是节点展开事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e) { TreeNode node = e.Node; if (node.Expanded == true) { string bm = e.Node.Value.ToString();//wsw更改 DataTable table = pagBLL.GetList(" superfuncid=" + bm + " order by sort asc").Tables[0]; node.ChildNodes.Clear(); //清除之前加载的空节点。 if (table.Rows.Count > 0) { for (int i = 0; i < table.Rows.Count; i++) { TreeNode Node = new TreeNode(); Node.Text = table.Rows[i]["funcname"].ToString(); Node.Value = table.Rows[i]["pagefuncid"].ToString(); Node.ToolTip = table.Rows[i]["funcurl"].ToString(); Node.ImageUrl = "../../" + table.Rows[i]["imageurl"].ToString(); node.ChildNodes.Add(Node);//增加父节点,这时直接往树TreeView1上加 this.LoadAddSign(Node.ChildNodes, Convert.ToInt32(table.Rows[i]["pagefuncid"].ToString()));//wsw更改 } } } } #endregion