• 动态生成树


    if (!IsPostBack) //如果是第一次加载页面
    {
       if (Session["TreeNod"] == null) //如果不存在菜单树缓存对象
       {
          BuildTreeview(); //调用生成菜单树
          if (tvNavigation.Nodes.Count > 0)
          {
             Session["TreeNod"] = tvNavigation.Nodes[0]; //将生成的菜单树缓存
          }
       }
       else //如果存在菜单树缓存对象
       {
          //从缓存对象中取出菜单树
          TreeNode Nod = (TreeNode)(Session["TreeNod"]);
          tvNavigation.Nodes.Add(Nod);
       }
    }

    #region 生成菜单树相关方法

    /// <summary>
    /// 递归生成菜单树
    /// </summary>
    private void BuildTreeview()
    {
        tvNavigation.Nodes.Clear();
       string conStr = ConfigurationManager.ConnectionStrings["ExpatiateAspNetConnectionString1"].ToString();
       DataClassesDataContext dc = new DataClassesDataContext(conStr);
       List<MenuInfo> menuList = dc.MenuInfo.Select(itm=>itm).ToList();
       List<MenuInfo> level1List = menuList.Where(itm => itm.MenuLevel == 1).ToList();
       foreach (MenuInfo level1 in level1List)
       {
          TreeNode nod = GetTreeNode(level1);
          BuildTreeNode(level1, nod, menuList);
          tvNavigation.Nodes.Add(nod);
        }

    }

    /// <summary>
    /// 递归实现函数
    /// </summary>
    /// <param name="curNod"></param>
    /// <param name="menuList"></param>
    private void BuildTreeNode(MenuInfo menu, TreeNode nod, List<MenuInfo> menuList)
    {
       List<MenuInfo> childMenuList = menuList.Where(itm => itm.ParentNode == menu.MenuCode).ToList();
       foreach (MenuInfo childMenu in childMenuList)
       {
          TreeNode childNod = GetTreeNode(childMenu);//调用自定义方法GetTreeNode获得节点对象
          nod.ChildNodes.Add(childNod);//将节点对象添加到TreeView控件中
          BuildTreeNode(childMenu, childNod, menuList);
       }
    }

    /// <summary>
    /// 根据菜单实体生成树节点实体
    /// </summary>
    /// <param name="menu"></param>
    /// <returns></returns>
    private TreeNode GetTreeNode(MenuInfo menu)
    {
       TreeNode nod = new TreeNode();
       nod.Value = menu.MenuID.ToString();//设置节点值属性
       nod.NavigateUrl = menu.Url; //设置节点的导航URL
       nod.Text = menu.Title; //设置节点显示的标题
       nod.ToolTip = menu.Description; //设置节点描述信息
       nod.Target = menu.Target;
       nod.ImageUrl = menu.ImgUrl; //设置节点左侧显示的图标
       return nod;
    }

    #endregion

  • 相关阅读:
    Rest API
    Spring webflux
    混合配置
    Profile
    Bean的作用域
    事件的监听与发布
    Condition
    完美解决 fatal: unable to access ‘https://github.com/.../.git‘: Could not resolve host: github.com
    virtualbox 给centos扩容方法
    Mac 安装mysqlclient报错 OSError: mysql_config not found
  • 原文地址:https://www.cnblogs.com/Yellowshorts/p/2867757.html
Copyright © 2020-2023  润新知