zoukankan      html  css  js  c++  java
  • C# 中Treeview无限级目录实现

    库表结构:

          后台代码:

        protected void Page_Load(object sender, EventArgs e)//页面打开时执行
        {
            if (!IsPostBack)
            {
                PopulateNodes(TreeView1.Nodes, 0);
            }
        }

        public DataTable GetTreeTable()//获得表
        {
            dbClient db = new dbClient();
            DataTable dt =  HttpContext.Current.Cache["Treeview"] as DataTable;//缓存表,保证速度
            string sql = "select id,pid,yhm from users";
            dt = db.getSQLTable(sql, "dt");  //获得表    
            return dt;
        }

        public void PopulateNodes(TreeNodeCollection nodes, Int32 intParentID)//节点展开
        {
            DataTable dt = GetTreeTable();
            String strExpression = String.Empty;
            strExpression = "[pid]=" + intParentID;
            System.Data.DataRow[] dr;
            dr = dt.Select(strExpression);
            for (Int32 i = 0; i <= dr.GetUpperBound(0); i++)
            {
                TreeNode tn = new TreeNode();
                tn.Text = dr[i]["yhm"].ToString().Trim();
                tn.Value = dr[i]["id"].ToString();
                tn.NavigateUrl = "Detail.aspx?id=" + tn.Value.ToString();//设置节点URL属性
                tn.Target = "mainFrame";//设置节点Target属性
                DataRow[] dr1; 
                dr1 = dt.Select("[pid]=" + tn.Value);
                if (dr1.GetUpperBound(0) > -1)
                {
                    tn.PopulateOnDemand = true;
                }
                nodes.Add(tn);
            }
        }

        protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)//触发节点展开
        {
            PopulateNodes(e.Node.ChildNodes, Int32.Parse(e.Node.Value));
        }

          前台只要拖个Treeview就可以了,然后捆绑下事件:
        
  • 相关阅读:
    php设计模式-工厂模式(一)
    php Pthread 线程 互斥锁
    php Pthread 多线程 Worker
    PHP多进程实例
    C# this 关键字
    .NET FileStream文件流,StreamReader文本流,MemoryStream内存流几种流的实例
    深入理解Static关键字修饰符
    SQL Server 视图
    .NET初识委托
    RSA加密
  • 原文地址:https://www.cnblogs.com/top5/p/1545884.html
Copyright © 2011-2022 走看看