• C#递归加载树


    递归,就是有去有回,自己调用自己。

     1 public partial class SiteMaster : MasterPage
     2     {
     3         public DataSet list = null;
     4 
     5         public List<TreeData> treeDatas = new List<TreeData>();
     6         string str = "";
     7         protected void Page_Load(object sender, EventArgs e)
     8         {
     9             if (LoginMITBz.currentUser == null)
    10             {
    11                 Response.Redirect("/Login/Login.aspx");
    12             }
    13             string sql = "SELECT a.Title,a.TreeId,a.Url,a.ParentID,a.ParentPath FROM dbo.M_Tree a INNER JOIN dbo.M_Permissions b ON a.TreeId = b.TreeId WHERE b.UserId=" + LoginMITBz.currentUser.UserId;
    14             list = Hy.DBSqlU.DBUtility_USA.DbHelperSQLDB_USA_MIT_MitInfo.Query(sql);
    15             #region Test
    16 
    17             //foreach (DataRow mDr in list.Tables[0].Rows)
    18             // {
    19             // DataColumn mDc = list.Tables[0].Columns[0];
    20             //Console.WriteLine(mDr["Title"].ToString());
    21             //Console.WriteLine(mDr["Url"].ToString());
    22 
    23             //    //foreach (DataColumn mDc in list.Tables[0].Columns)
    24             //    //{
    25             //    //    Console.WriteLine(mDr["Title"].ToString());
    26             //    //    Console.WriteLine(mDr["Url"].ToString());
    27             //    //}
    28             //}
    29             #endregion
    30 
    31             foreach (DataRow mDr in list.Tables[0].Rows)
    32             {
    33                 DataColumn mDc = list.Tables[0].Columns[0];
    34                 TreeData tree = new TreeData();
    35                 tree.TreeId = (long)mDr["TreeId"];
    36                 tree.Title = mDr["Title"].ToString();
    37                 tree.Url = mDr["Url"].ToString();
    38                 tree.ParentId = (int)mDr["ParentID"];
    39                 treeDatas.Add(tree);
    40             }
    41 
    42            
    43             var data = treeDatas.Where(a => a.ParentId == 0);
    44             foreach (var item in data)
    45             {
    46                 str += item.Title + ":" + item.Url + "
    ";
    47                 GetNode((int)item.TreeId);
    48             }
    49         }
    50         
    51         public  void GetNode(int parentId)
    52         {
    53             var data = treeDatas.Where(a => a.ParentId == parentId);
    54             foreach (var item in data)
    55             {
    56                 str += item.Title + ":" + item.Url + "
    ";
    57                 GetNode((int)item.TreeId);
    58             }
    59         }
    60     }
    61     
    62 
    63 
    64     public class TreeData
    65     {
    66         public long? TreeId { get; set; }
    67         public int? ParentId { get; set; }
    68         public string Title { get; set; }
    69         public string Url { get; set; }
    70     }

  • 相关阅读:
    Vue scoped lang
    Vue生命周期
    js 面试题 a.x = a = { n: 2 }
    Leaflet中对鼠标按下、移动事件监听实现移动单个图形
    Leaflet中使用Leaflet.draw插件实现图形交互绘制和编辑(修改图形坐标点)
    C/C++ 实现正反向端口映射
    C/C++ ShellCode 常用加密方式
    FPS游戏:实现人物定点瞬移
    达梦数据库:第三章:报错有歧义的列名[PRIVILEGE_NAME]
    达梦数据库,第二章:报错不是 GROUP BY 表达式
  • 原文地址:https://www.cnblogs.com/gyl5201314/p/9154514.html
Copyright © 2020-2023  润新知