tree加载《笔记》

逐级加载

.aspx:

 

 <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="tvDepartment" runat="server"
         NodeIndent="10" OnTreeNodePopulate="tvDepartment_TreeNodePopulate"
            EnableClientScript="false" ShowLines="True">
        <ParentNodeStyle ForeColor="#5555DD" />
        <SelectedNodeStyle BackColor="#f2f6fb" />
        <NodeStyle Font-Size="12px" ForeColor="Black" />
       
        </asp:TreeView>
    </div>
    </form>

 

.cs:

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace UpFileDemo.Tree
{
    public partial class ITreeView1 : System.Web.UI.Page
    {
        private static Dictionary<string, IList<CCItem>> nodeDictionary = new Dictionary<string, IList<CCItem>>();

        public class CCItem {
            private string name;

            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            private string value;

            public string Value
            {
                get { return this.value; }
                set { this.value = value; }
            }

        }
          protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                BindDepartmentTree("1000");
            }
        }
   
        //取得根目录的第一级数据
        public void BindDepartmentTree(string pwPid)
        {
            nodeDictionary.Clear();
            CCItem userpower = new CCItem();
            //取得根目录         

            string strsql = "SELECT [wf_SysFuns_ID]      ,[wf_SysFuns_name]      ,[wf_SysFuns_desc]      ,[wf_SysFuns_floor]"
                      + "    ,[wf_SysFuns_url]      ,[wf_SysFuns_target]      ,[wf_SysFuns_image] "
                      + " FROM [wf_SysFuns] where  wf_SysFuns_floor like '" + pwPid + "'"; // " and len(wf_SysFuns_floor)=" + (pwPid.Length + 4);
            System.Collections.Generic.List<CCItem> list = new System.Collections.Generic.List<CCItem>();
            list = getCCItemList(strsql);
            tvDepartment.Nodes.Clear();
            if (list == null) return;

            TreeNode tnRootNode = new TreeNode();
            tnRootNode.Text = list[0].Name;
            tnRootNode.Value = list[0].Value; ;
             string strsql2 = "SELECT [wf_SysFuns_ID]      ,[wf_SysFuns_name]      ,[wf_SysFuns_desc]      ,[wf_SysFuns_floor]"
                   + "    ,[wf_SysFuns_url]      ,[wf_SysFuns_target]      ,[wf_SysFuns_image] "
                   + " FROM [wf_SysFuns] where  wf_SysFuns_floor like '" + pwPid
                   + "%'  and len(wf_SysFuns_floor)=" + (pwPid.Length + 4);
            list = new System.Collections.Generic.List<CCItem>();
            list = getCCItemList(strsql2);
            if (list == null || list.Count < 1)
            {
                return;
            }
            else
            {
                foreach (CCItem item in list)
                {

                    TreeNode tnNewNode = new TreeNode(item.Name, item.Value);
                    string strsql3 = "SELECT [wf_SysFuns_ID]      ,[wf_SysFuns_name]      ,[wf_SysFuns_desc]      ,[wf_SysFuns_floor]"
                 + "    ,[wf_SysFuns_url]      ,[wf_SysFuns_target]      ,[wf_SysFuns_image] "
                 + " FROM [wf_SysFuns] where  wf_SysFuns_floor like '" + item.Value
                 + "%'  and len(wf_SysFuns_floor)=" + ((item.Value).Length + 4);
                    System.Collections.Generic.List<CCItem> ilChilldDep = getCCItemList(strsql3);
                    tnRootNode.ChildNodes.Add(tnNewNode);
                    if (ilChilldDep == null || ilChilldDep.Count < 1) { return; }
                    else
                    {
                        tnNewNode.PopulateOnDemand = true;
                        tnNewNode.Expanded = false;
                        nodeDictionary.Add(item.Value,ilChilldDep);
                    }
                }
                tvDepartment.Nodes.Add(tnRootNode);
            }
        }

        protected void tvDepartment_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            if (tvDepartment.SelectedNode != null)
            {
                string sValue = tvDepartment.SelectedNode.Value;
            }

            string nodeID = e.Node.Value;
            if (nodeDictionary != null)
            {
                if (nodeDictionary.Keys.Contains(nodeID))
                {
                    if (nodeDictionary[nodeID].Count > 0)
                    {
                        foreach (CCItem item in nodeDictionary[nodeID])
                        {
                            TreeNode tnNewNode = new TreeNode(item.Name, item.Value);
                            string strsql4 = "SELECT [wf_SysFuns_ID]      ,[wf_SysFuns_name]      ,[wf_SysFuns_desc]      ,[wf_SysFuns_floor]"
          + "    ,[wf_SysFuns_url]      ,[wf_SysFuns_target]      ,[wf_SysFuns_image] "
          + " FROM [wf_SysFuns] where  wf_SysFuns_floor like '" + item.Value
          + "%'  and len(wf_SysFuns_floor)=" + ((item.Value).Length + 4);
                            System.Collections.Generic.List<CCItem> ccItemList = getCCItemList(strsql4);
                            if (ccItemList == null || ccItemList.Count < 1)
                            {
                                tnNewNode.Expanded = false;
                                tnNewNode.PopulateOnDemand = false;
                                tnNewNode.SelectAction = TreeNodeSelectAction.Select;
                                e.Node.ChildNodes.Add(tnNewNode);
                            }
                            else
                            {
                                tnNewNode.PopulateOnDemand = true;
                                tnNewNode.Expanded = false;
                                tnNewNode.SelectAction = TreeNodeSelectAction.Select;
                                e.Node.ChildNodes.Add(tnNewNode);
                                nodeDictionary.Add(item.Value, ccItemList);
                            }
                        }
                        nodeDictionary.Remove(nodeID);
                    }

                }

            }
        }

        private System.Collections.Generic.List<CCItem> getCCItemList(string strsql)
        {
            DataTable dt = new DataTable();
            WoPrj.DataConfig.Open();
            DataSet ds = new DataSet();
            WoPrj.DataBase.RetDataSet(WoPrj.DataConfig.Conn, strsql, CommandType.Text, null, ref ds);
            WoPrj.DataConfig.Close();
            dt = ds.Tables[0];
            System.Collections.Generic.List<CCItem> list = new System.Collections.Generic.List<CCItem>();
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CCItem item = new CCItem();
                    item.Name = dt.Rows[i]["wf_SysFuns_name"].ToString();
                    item.Value = dt.Rows[i]["wf_SysFuns_floor"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }
    }
}
=========================================================

一次性加载,展开到固定级别;

.cs:

 

    public partial class IForTree : System.Web.UI.Page
    {
        int iflag = 5;
  
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string PopId = "1000";
                bindtree(PopId);
             
            }
        }
         private void bindtree(string ParentID)
        {
            TreeView1.Nodes.Clear();
            int ii = 0;
            AddTree(ParentID, (TreeNode)null,ii);
        }
         public void AddTree(string ParentID, TreeNode pNode,int ii)
        {
         
            DataTable dt = new DataTable();
            string strsql = "SELECT [wf_SysFuns_ID]      ,[wf_SysFuns_name]      ,[wf_SysFuns_desc]      ,[wf_SysFuns_floor]"
       + "      ,[wf_SysFuns_url]      ,[wf_SysFuns_target]      ,[wf_SysFuns_image]  FROM [wf_SysFuns] ";
            WoPrj.DataConfig.Open();
            DataSet ds = new DataSet();
            WoPrj.DataBase.RetDataSet(WoPrj.DataConfig.Conn, strsql, CommandType.Text, null,ref ds);
            WoPrj.DataConfig.Close();
            dt = ds.Tables[0]; 

            DataView dvTree = new DataView(dt);
            if (ii == 0)
            {
                dvTree.RowFilter = " wf_SysFuns_floor like '" + ParentID + "%' and len(wf_SysFuns_floor)=" + (ParentID.Length);
            }
            else
            {
                dvTree.RowFilter = " wf_SysFuns_floor like '" + ParentID + "%' and len(wf_SysFuns_floor)=" + (ParentID.Length + 4);

            }
            ii++;

            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                if (pNode == null)
                {
                    Node.Text = Row["wf_SysFuns_name"].ToString();
                    TreeView1.Nodes.Add(Node);
                    if (iflag > ii)
                    {
                        Node.Expanded = true;
                    }
                    else {
                        Node.Expanded = false;
                    }
                    AddTree(Row["wf_SysFuns_floor"].ToString(), Node,ii); //再次递归
                }
                else
                { 
                    Node.Text = Row["wf_SysFuns_name"].ToString();
                    Node.NavigateUrl = Row["wf_SysFuns_url"].ToString();
                    pNode.ChildNodes.Add(Node);
                    if (iflag > ii)
                    {
                        Node.Expanded = true;
                    }
                    else
                    {
                        Node.Expanded = false;
                    }
                    AddTree(Row["wf_SysFuns_floor"].ToString(), Node,ii); //再次递归
                }
            }
        
        }
 
    

    }

 

 

 

posted @ 2011-06-13 00:12  】Richard【  阅读(227)  评论(0编辑  收藏  举报