• 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就可以了,然后捆绑下事件:
        
  • 相关阅读:
    Django基础六之ORM中的锁和事务
    Django学习之model进阶
    Django基础八之cookie和session
    Django基础五之django模型层(二)多表操作
    Django基础五之django模型层(一)单表操作
    Django基础四之模板系统
    webpack最佳入门实践系列(4)
    webpack最佳入门实践系列(3)
    webpack最佳入门实践系列(2)
    webpack最佳入门实践系列(1)
  • 原文地址:https://www.cnblogs.com/top5/p/1545884.html
Copyright © 2020-2023  润新知