zoukankan      html  css  js  c++  java
  • 仿Windows制作TreeView数据加载

      时间有限就直接贴源码吧,理解思路即可。

    页面代码:

    <asp:TreeView ID="TreeViewLeft" runat="server" ShowLines="True" ExpandDepth="2" 
         Target="mainFrame" ontreenodeexpanded="TreeViewLeft_TreeNodeExpanded1">
    </asp:TreeView>
    

    .cs文件代码:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AddNode(6160);  //父节点Id
            }
        }
        #region 页面加载时,加载第0级和第1级节点
        private void AddNode(int parentId)
        {
            string UserId = HttpContext.Current.Session["zhy_UserId"].ToString();
            //通过传进来的参数,得到数据集
            string strSQL = "select * from "
                    + "(SELECT id,PageRegionId,Name, HyperLinkPage,Target,ParentID, [RolesAllow],IsPublic, "
                    + "[RolesUnallow], [UsersAllow], (',' + [UsersUnallow]) as [UsersUnallow],[Sort] "
                    + "FROM [zhy_AdminMenu] "
                    + "WHERE [ClassID] = 0 And DeleteSign=0 AND [IsPublic] IN (0,1) AND IsStage=2 AND [PARENTID]=@parentId) as a "
                    + "where  [UsersUnallow] is null or ([UsersUnallow] is not null and [UsersUnallow] not like '%" + "," + UserId + "," + "%') "
                    + "ORDER BY [ParentID] ASC, [Sort] DESC ";
            if (Session["IsProgrammer"].ToString() == "1")
            {
                strSQL = "select * from "
                    + "(SELECT id,PageRegionId,Name, HyperLinkPage,Target,ParentID, [RolesAllow],IsPublic, "
                    + "[RolesUnallow], [UsersAllow], (',' + [UsersUnallow]) as [UsersUnallow],[Sort] "
                    + "FROM [zhy_AdminMenu] "
                    + "WHERE [ClassID] = 0 And DeleteSign=0 AND IsStage=2 AND [PARENTID]=@parentId) as a "
                    + "where [UsersUnallow] is null or ([UsersUnallow] is not null and [UsersUnallow] not like '%" + "," + UserId + "," + "%') "
                    + "ORDER BY [ParentID] ASC, [Sort] DESC ";
            }
            SqlParameter[] paras = new SqlParameter[]{
                                                            new SqlParameter("@parentId",parentId)
                                                         };
            //Response.Write(strSQL);
            //Response.End();
            DataSet ds = GetDataSet(strSQL, paras);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if ((int)ds.Tables[0].Rows[i]["IsPublic"] != 1 && (int)ds.Tables[0].Rows[i]["IsPublic"] != 2)
                {
                    if (zhyPurview.PageRegionValidateRolesUnallow(ds.Tables[0].Rows[i]["RolesUnallow"].ToString(), UserId))
                    {
                        ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[i]);
                        i--;
                    }
                    else if (!zhyPurview.PageRegionValidateRolesAllow(ds.Tables[0].Rows[i]["RolesAllow"].ToString(), UserId) && !zhyPurview.PageRegionValidateUsersAllow(ds.Tables[0].Rows[i]["UsersAllow"].ToString(), UserId))
                    {
                        ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[i]);
                        i--;
                    }
                }
            }
    
            //循环遍历,加载所有0级节点和1级节点
            foreach (DataRow Row in ds.Tables[0].Rows)
            {
                TreeNode Node = new TreeNode();
                Node.Text = Row["Name"].ToString();
                Node.SelectAction = TreeNodeSelectAction.Expand;
    
                Node.Value = Row["id"].ToString();
                Node.NavigateUrl = Row["HyperLinkPage"].ToString();
                if (Row["Target"].ToString() == "1")
                {
                    Node.Target = "_blank";
                }
                TreeViewLeft.ExpandDepth = 0;
                TreeViewLeft.Nodes.Add(Node);
                AddChildNodes(Node);    //再次递归
            }
        }
        #endregion
    
        #region 增加子节点
        public void AddChildNodes(TreeNode pNode)
        {
            string UserId = HttpContext.Current.Session["zhy_UserId"].ToString();
            //通过传进来的参数,得到所有子节点的数据集
            string strSQL = "select * from "
                    + "(SELECT id,PageRegionId,Name, HyperLinkPage,Target,ParentID, [RolesAllow],IsPublic, "
                    + "[RolesUnallow], [UsersAllow], (',' + [UsersUnallow]) as [UsersUnallow],[Sort] "
                    + "FROM [zhy_AdminMenu] "
                    + "WHERE [ClassID] = 0 And DeleteSign=0 AND [IsPublic] IN (0,1) AND IsStage=2 AND [PARENTID]=@parentId) as a "
                    + "where  [UsersUnallow] is null or ([UsersUnallow] is not null and [UsersUnallow] not like '%" + "," + UserId + "," + "%') "
                    + "ORDER BY [ParentID] ASC, [Sort] DESC ";
            if (Session["IsProgrammer"].ToString() == "1")
            {
                strSQL = "select * from "
                    + "(SELECT id,PageRegionId,Name, HyperLinkPage,Target,ParentID, [RolesAllow],IsPublic, "
                    + "[RolesUnallow], [UsersAllow], (',' + [UsersUnallow]) as [UsersUnallow],[Sort] "
                    + "FROM [zhy_AdminMenu] "
                    + "WHERE [ClassID] = 0 And DeleteSign=0 AND IsStage=2 AND [PARENTID]=@parentId) as a "
                    + "where [UsersUnallow] is null or ([UsersUnallow] is not null and [UsersUnallow] not like '%" + "," + UserId + "," + "%') "
                    + "ORDER BY [ParentID] ASC, [Sort] DESC ";
            }
            SqlParameter[] paras = new SqlParameter[]
                    {
                        new SqlParameter("@parentId",pNode.Value)
                    };
            DataSet ds = GetDataSet(strSQL, paras);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if ((int)ds.Tables[0].Rows[i]["IsPublic"] != 1 && (int)ds.Tables[0].Rows[i]["IsPublic"] != 2)
                {
                    if (zhyPurview.PageRegionValidateRolesUnallow(ds.Tables[0].Rows[i]["RolesUnallow"].ToString(), UserId))
                    {
                        ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[i]);
                        i--;
                    }
                    else if (!zhyPurview.PageRegionValidateRolesAllow(ds.Tables[0].Rows[i]["RolesAllow"].ToString(), UserId) && !zhyPurview.PageRegionValidateUsersAllow(ds.Tables[0].Rows[i]["UsersAllow"].ToString(), UserId))
                    {
                        ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[i]);
                        i--;
                    }
                }
            }
    
            foreach (DataRow Row in ds.Tables[0].Rows)
            {
                TreeNode Node = new TreeNode();
                Node.Value = Row["id"].ToString();
                Node.Text = Row["Name"].ToString();
    
                Node.SelectAction = TreeNodeSelectAction.Expand;
                Node.NavigateUrl = Row["HyperLinkPage"].ToString();
                if (Row["Target"].ToString() == "1")
                {
                    Node.Target = "_blank";
                }
                pNode.ChildNodes.Add(Node);
                pNode.NavigateUrl = "";
            }
        }
        #endregion
    
        #region 通过sql语句,得到数据集
        public static DataSet GetDataSet(string strSQL, params SqlParameter[] values)
        {
            try
            {
    
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NMXT_DBConn"].ToString()))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = strSQL;
                    cmd.CommandTimeout = 60;
                    cmd.Parameters.AddRange(values);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    return ds;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(values[0].Value + "——" + strSQL);
            }
        }
        #endregion
    
        #region 当点击第0级节点的时候,加载第1级的所有子节点
    
        protected void TreeViewLeft_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            foreach (TreeNode subNode in e.Node.ChildNodes)
            {
                if (subNode.ChildNodes.Count <= 0)
                {
                    AddChildNodes(subNode);
                }
            }
        }
    
        #endregion
    

      

  • 相关阅读:
    重要网址(工具的使用)
    navicat的使用(测试库和正式库同步)以及用plsql改表字段属性
    linux
    linux一些基本命令
    LINUX的一些基本概念和操作
    LINUX简介
    Redis系统管理
    Redis之数据类型
    Redis的简介与安装
    论文阅读笔记(二十七)【AAAI2019】:A Bottom-Up Clustering Approach to Unsupervised Person Re-Identification
  • 原文地址:https://www.cnblogs.com/stevenjson/p/3432917.html
Copyright © 2011-2022 走看看