• 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就可以了,然后捆绑下事件:
        
  • 相关阅读:
    Git 修改已提交的commit注释
    设置git bash中显示行号等
    JS 获取字符串长度
    泛型接口
    约束
    泛型方法
    泛型
    重载运算符
    自定义转换
    装箱和拆箱
  • 原文地址:https://www.cnblogs.com/top5/p/1545884.html
Copyright © 2020-2023  润新知