多级目录树

前台:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>选择目录树</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
<asp:treeview runat="server" ID="tvMenu" ImageSet="Contacts"
            onselectednodechanged="tvMenu_SelectedNodeChanged" Width="266px" NodeIndent="10">
    <HoverNodeStyle Font-Underline="False" />
 
    <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
        HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
    <ParentNodeStyle Font-Bold="True" ForeColor="#5555DD" />
    <SelectedNodeStyle Font-Underline="True"
        HorizontalPadding="0px" VerticalPadding="0px" />
        </asp:treeview>
    </div>
    </form>
</body>
</html>

后台:

public partial class menu : System.Web.UI.Page
    {
        private SqlConnection conn;
        private SqlCommand cmd;
        private const string connStr = "Server=115.29.244.185,9838;initial catalog=DbDevice;User=prkang_v2_access;Pwd=password01!";
        private string type = string.Empty;

protected void Page_Load(object sender, EventArgs e)
        {
            loadDefault();
        }

void loadDefault()
        {
            //加载目录树

DataTable dtable0 = GetData0();
            DataTable dtable = GetData1();
            DataTable dtable2 = GetData2();
            CreateTree(this.tvMenu,dtable0, dtable, dtable2);
        }

protected void tvMenu_SelectedNodeChanged(object sender, EventArgs e)
        {
            Session["MenuId"] = string.Empty;
            if (this.tvMenu.SelectedNode.Value != null)
            {
                Session["MenuId"] = this.tvMenu.SelectedValue;
            }

Response.Write("<script>window.close();window.opener.reflash();window.opener=null;</script>");
            //前我做能脚本某写错需要测试
        }

private DataTable GetData0()
        {
            DataTable datatable = GetTable("select * from PB_STAFF");
            return datatable;
        }
          private DataTable GetData1()
        {
            DataTable dt = GetTable("select * from Tbl_Dealer");
            return dt;
        }

private DataTable GetData2()
        {
            DataTable dt = GetTable("select * from Tbl_Terminal");
            return dt;
        }
  
        #region  创建树、节点
        /// <summary> 
        /// 创建树 
        /// </summary>  
        /// <param name="treeView"></param>
        /// <param name="treeData"></param> 
        private void CreateTree(TreeView treeView,DataTable pbData, DataTable treeData, DataTable subdata)
        {
            try
            {
                var ss = SessionUtil.GetStaffSession();

var model = PRKEP.Data.PB_STAFF.SingleOrDefault(x => x.PB_LOGIN_ID == ss.LoginId);
                string dname = model.PB_NAME;
                string DealerId = model.PB_LOGIN_ID;
                treeView.Nodes.Clear();//清空树所节点        
                treeView.ExpandDepth = 10;
                TreeNode rootNode = new TreeNode();//创建根节点     
                rootNode.Text = dname;
                rootNode.Value = DealerId;
                rootNode.ImageUrl = "../../Images/icon_remind.gif";
                treeView.Nodes.Add(rootNode);//添加根节点 
                CreateChildNode(rootNode,pbData,treeData, subdata);//创建其节点

}
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "fault", "alert(‘" + ex.Message.Replace("\n", "").Replace("\r", "") + "‘);", true);
            }
        }

#endregion
        #region 创建销售节点
        /// <summary>
        /// 创建销售节点
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="treeData"></param>
        /// <param name="subData"></param>
        private static void CreateChildNode(TreeNode parentNode,DataTable pbData ,DataTable treeData, DataTable subData)
        {
            try
            {
                DataRow[] rowList = pbData.Select("PB_PARENT_ID=‘" + parentNode.Value + "‘");
                foreach (DataRow row in rowList)
                {
                    TreeNode node = new TreeNode();//创建新节点

node.Text = row["PB_NAME"].ToString();
                    node.Value = row["PB_LOGIN_ID"].ToString();
                    parentNode.ChildNodes.Add(node);
                    CreateChildNode(node, pbData, treeData, subData);//递归调用创建其销售员节点

string exp2 = "ParentId=‘"+node.Value+"‘";

DataRow[] rowlist2 = treeData.Select(exp2);
                    if (rowlist2 != null)
                    {
                        foreach (DataRow rows in rowlist2)
                        {
                            TreeNode node1 = new TreeNode();//创建新节点

node1.Value = rows["DealerId"].ToString();
                            node1.Text = rows["DealerName"].ToString();
                            parentNode.ChildNodes.Add(node1);
                            CreatexiaoshouChildNode(node1, treeData);//递归调用创建其经销商节点

string exp3 = "DealerId=‘" + node1.Value + "‘";
                            DataRow[] rowlist3 = subData.Select(exp3);

if (rowlist3 != null)
                            {
                                foreach (DataRow rowlist in rowlist3)
                                {
                                    TreeNode node2 = new TreeNode();
                                    node2.Value = rows["TerminalId"].ToString();
                                    node2.Text = rows["TerminalName"].ToString();
                                    parentNode.ChildNodes.Add(node2);
                                    CreateSubChildNode(node2, subData);
                                }
                            }
                        }
                    }
                  
                }

}
            catch (Exception ex)
            {
                throw ex;
            }
        }

#endregion
        #region 创建销售人员下属的销售人员
        /// <summary>
        /// 创建销售人员下属的销售人员
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="subtreeData"></param>
        private static void CreatexiaoshouChildNode(TreeNode parentNode, DataTable treeData)
        {
            try
            {
                DataRow[] rowlist2 = treeData.Select("ParentId=‘" + parentNode.Value + "‘");
                if (rowlist2 != null)
                {
                    foreach (DataRow row1 in rowlist2)
                    {
                        TreeNode node1 = new TreeNode();//创建新节点
                        node1.Text = row1["DealerName"].ToString();
                        node1.Value = row1["DealerId"].ToString();
                        parentNode.ChildNodes.Add(node1);
                        CreatexiaoshouChildNode(node1, treeData);//递归调用创建其节点  
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

#endregion

#region  创建经销商下属的终端客户节点
        /// <summary>
        /// 创建经销商下属的终端客户节点
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="subtreeData"></param>
        private static void CreateSubChildNode(TreeNode parentNode, DataTable subtreeData)
        {
            try
            {
                DataRow[] rowlist2 = subtreeData.Select("DealerId=‘" + parentNode.Value + "‘");
                if (rowlist2 != null)
                {
                    foreach (DataRow row1 in rowlist2)
                    {
                        TreeNode node1 = new TreeNode();//创建新节点
                        node1.Text = row1["TerminalName"].ToString();
                        node1.Value = row1["TerminalId"].ToString();
                        parentNode.ChildNodes.Add(node1);
                        CreateSubChildNode(node1, subtreeData);//递归调用创建其节点  
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

#region  获取经销商对应的级别名称
        /// <summary>
        /// 获取经销商对应的级别名称
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        private static string getDealerLevelName(string level)
        {
            string str = string.Empty;
            switch (level)
            {
                case "0":
                    str = "钻石";
                    break;
                case "1":
                    str = "金牌";
                    break;
                case "2":
                    str = "银牌";
                    break;
                default:
                    str = "铜牌";
                    break;
            }
            return str;

}

#endregion
        #region 数据库操作
        /// <summary>  
        /// 打链接   
        /// </summary>    
        ///<returns></returns> 
        private SqlConnection GetConn()
        {
            if (conn == null)
                conn = new SqlConnection(connStr);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            else if (conn.State == ConnectionState.Broken)
            {
                conn.Close();
                conn.Open();
            } return conn;
        }
        /// <summary>   
        /// 执行增删改查操作
        /// </summary>  
        /// <param name="sql"></param> 
        /// <returns></returns> 
        private int ExecuteNonQuery(string sql)
        {
            try
            {
                cmd = new SqlCommand(sql, GetConn());
                return cmd.ExecuteNonQuery();
            }
            catch
            {
                return 0;
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>   
        /// 读数据
        /// </summary>  
        /// <param name="sql"></param> 
        /// <returns></returns>  
        private SqlDataReader ExecuteReader(string sql)
        {
            try
            {
                cmd = new SqlCommand(sql, GetConn());
                return cmd.ExecuteReader();
            }
            catch
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }

/// <summary>    
        /// 该表数据 
        /// </summary> 
        /// <param name="sql"></param>   
        /// <returns></returns>
        private DataTable GetTable(string sql)
        {
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(sql, GetConn());
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds.Tables[0];
            }
            catch
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }
        #endregion

}

多级目录树,布布扣,bubuko.com

时间: 2024-10-16 18:47:55

多级目录树的相关文章

python 小程序 复制目录树

1. 将一个目录树完全复制到另外一个目录下面 import os, sys """ 复制目录树 """ maxloadsize = 1024 * 1024 def copyfile(frompath, topath, maxloadsize= maxloadsize): """ 单个文件的复制 :param frompath:原文件 :param topath: 复制到的文件 :param maxloadsize:

将多级目录打包成zip文件

最近接触PHP,需要用到zip压缩,在网上搜索的一大堆,发现代码都不低于50行.  而且调用还很费事(基础太少看不懂).让我收获的是Php提供有一个ZipArchive类,并有如下方法. bool addEmptyDir( string $dirname ) bool addFile ( string $filename [, string$localname = NULL[, int$start = 0 [, int $length = 0 ]]] ) mixed open( string $

【Lua】Lua + LWT + ExtJS构建目录树

Lua处理后台逻辑,Lua lwt搭建后台程序,ExtJS根据后台传来的json数据构建目录树. 前台html和ExtJS代码不用多讲,直接上代码: treePanel.html 1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4 5 <title>ExtJS TreePanel</

php创建多级目录完整封装类操作

创建多级目录函数中调用创建指定下的指定文件的函数: public function create_dir($dir,$mode=0777) { return is_dir($dir) or ($this->create_dir(dirname($dir)) and mkdir($dir, $mode)); } 创建指定路径下的指定文件,string $path(需要包含文件名和后缀),boolean $over_write 是否覆盖文件,int $time 设置时间.默认是当前系统时间,int

Python3 使用基本循环实现多级目录(思路)

一.多级目录设计: 1. 通过循环的方式显示菜单和进入菜单 2. 设置标志位以提供回退上一层菜单 2. 设置标志位以提供退出程序 二.注意要点: 1. 菜单样式,层次关系不要弄混乱 2. 当输入错误时,保留在当前view之下 3. 注意标志位使用之后对其他循环是否有影响,或者需要走其他分支

python【使用简单的os.walk生成目录树】

思路比较简单,使用os.walk 生成目录信息,然后再进行字符串操作,所以没有其他比较复杂的逻辑,但是生成的效果也比较简单,自己看看到是可以哈. 代码如下 # -*- coding: utf-8 -*- ''' Created on July 22, 2017 @author: hehe1234567 ''' import os def simple_dir_tree(ddir): for dirpath,dirnames,filenames in os.walk(ddir.strip(os.s

MVC 多级目录菜单

MVC多级目录菜单  ----- 简单模拟 Model ---- cs { public class Class1 { public int ID{get;set;} public int parentID { get; set; } public int childID { get; set; } public string title { get; set; } } MVC View ---- cshtml: @using WebApplication1.Models; @{ ViewBag

Linux学习笔记之(2)~linux目录树概览和说明

献给知道mono,了解Jexus,对.net混搭技术感兴趣的朋友. linux目录树如下: 详解:/bin: 系统有很多放置执行档的目录,但/bin比较特殊.因为/bin放置的是在单人维护模式下还能够被操作的指令.在/bin底下的指令可以被root与一般帐号所使用,主要有:cat, chmod, chown, date, mv, mkdir, cp, bash等等常用的指令./boot: 这个目录主要在放置开机会使用到的档案,包括Linux核心档案以及开机选单与开机所需设定档等等. Linux

利用jQuery无插件创建可扩展目录树

这是一个简单.轻量级的,基于jQuery的目录树.纯html代码. <html> <head> </head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>   <script> $( document ).ready( function( ) {     $( '.t