示例#1
0
        /// <summary>
        /// 递归生成树的HTML代码
        /// </summary>
        /// <param name="node"></param>
        /// <param name="checkOrRadio"></param>
        /// <returns></returns>
        public static string GenerateTree(TreeNode node, CheckOrRadio checkOrRadio)
        {
            if (node == null) return "无节点";
            TagBuilder liBuilder = new TagBuilder("li");
            TagBuilder spanBuilder = new TagBuilder("span")
            {
                InnerHtml = node.Title,
            };

            //是否有<input>选择框
            if (checkOrRadio != CheckOrRadio.No)
            {
                TagBuilder checkBoxBuilder = new TagBuilder("input");
                checkBoxBuilder.Attributes["type"] = checkOrRadio.ToString();
                checkBoxBuilder.Attributes["name"] = "parentid";
                checkBoxBuilder.Attributes["id"] = node.Id;
                checkBoxBuilder.Attributes["value"] = node.Id;
                checkBoxBuilder.Attributes["flag"] = node.Flag;
                if (node.IsSelected)
                {
                    checkBoxBuilder.Attributes["checked"] = "checked";
                }
                liBuilder.InnerHtml = "\r\n" + checkBoxBuilder.ToString() + "\r\n" + spanBuilder.ToString();
            }
            else
            {
                liBuilder.InnerHtml = "\r\n" + spanBuilder.ToString();
            }

            if (node.ChildList.Count > 0)
            {
                TagBuilder ulBuilder = new TagBuilder("ul");
                ulBuilder.AddCssClass("lastclass");

                foreach (TreeNode n in node.ChildList)
                {
                    ulBuilder.InnerHtml += "\r\n" + GenerateTree(n, checkOrRadio);
                }

                liBuilder.InnerHtml += "\r\n" + ulBuilder.ToString();
            }

            return liBuilder.ToString();
        }
示例#2
0
 /// <summary>
 /// 递归生成树的HTML代码
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 public static string GenerateTree(TreeNode node)
 {
     return GenerateTree(node, CheckOrRadio.CheckBox);
 }
示例#3
0
 /// <summary>
 /// 根据任一节点查找树的根节点并生成一颗完整树
 /// </summary>
 /// <param name="node"></param>
 /// <param name="nodeList"></param>
 /// <param name="isfindParent"></param>
 /// <param name="rootNode"></param>
 public static void GetTreeNodes(TreeNode node, List<TreeNode> nodeList, bool isfindParent, ref TreeNode rootNode)
 {
     if (node == null) return;
     if ((node.Parent == null && string.IsNullOrEmpty(node.ParentId)) || isfindParent)
     {
         node.ChildList = nodeList.Where(n => (n.ParentId == node.Id && !string.IsNullOrEmpty(n.ParentId))
                                           || (n.Parent == node && n.Parent != null)).ToList();
         if (!isfindParent)
             rootNode = node;
         foreach (TreeNode n in node.ChildList)
         {
             n.Parent = node;
             GetTreeNodes(n, nodeList, true, ref rootNode);
         }
     }
     else
     {
         if (node.Parent == null && node.ParentId != null)
             node.Parent = nodeList.SingleOrDefault(n=>n.Id == node.ParentId);
         GetTreeNodes(node.Parent, nodeList, false, ref  rootNode);
     }
 }