无限级递归生成HTML示例及ListBox,DropDownList等无限树

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="htmlTree.aspx.cs" Inherits="htmlTree" %> <!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>HTML无限层级目录树</title> <style type="text/css"> /* table css */ .tableCss { width:100%; border-collapse:collapse; font-size:12px; border:1px solid #d8d5d5; color: #333; margin-top:12px; } .tableCss p{ padding-bottom:10px;} .tableCss th { background-color:#5D7B9D; color:#ffffff; text-align:center; line-height:20px; border-collapse:collapse; } .tableCss td { padding:5px; border: 1px solid #D4D0C8; border-collapse:collapse; } .tableCss .tdHead { border-collapse:collapse; text-align: left; background-color:#E0ECFF; } .tableCss input[type="text"]{ border:1px solid #d8d5d5;color:#272727; height:18px; padding-top:2px; } .tableCss input[type="password"]{ border:1px solid #d8d5d5; color:#272727; height:18px; padding-top:2px; } .tableCss .inputBtn{border: 1px solid #6883AA; background-color:#748FB4; vertical-align: bottom;padding:3px 8px 3px 8px; color:#fff; cursor:pointer;} .tableCss input[type="radio"]{border:0px; font-size:12px; color:#272727;} .tableCss textarea{color: #272727; border:1px solid #d8d5d5;} .inputBtn2{border: 1px solid #6883AA; background-color:#748FB4; vertical-align: bottom;padding:3px 8px 3px 8px; color:#fff; cursor:pointer;} /* table css */ </style> </head> <body> <form runat="server"> <div> <asp:Literal runat="server"></asp:Literal> </div> </form> </body> </html> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; public partial class htmlTree : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { LiteralTree.Text = showtree(); } } #region 显示权限树 private int S_Isroot; private string NS_Name; private string NS_Id; private string trees; private StringBuilder BuilderBody = new StringBuilder(); private DataTable dt = createDT(); public string showtree() { StringBuilder Builder = new StringBuilder(); Builder.AppendLine("<table id=http://www.likecs.com/\'tableAction\' class=http://www.likecs.com/\'tableCss\'>"); Builder.AppendLine("<th>功能</th><th>父类编码</th><th>操作编码</th><th></th>"); showtreeforline(dt, "0", ""); Builder.Append(BuilderBody); Builder.AppendLine("</table>"); return Builder.ToString(); } #endregion #region 递归显示树 public void showtreeforline(DataTable dt, string moduleid, string tree) { bool ParentBool = true; //是否父节点 if (tree.Length == 0 || tree == "" || tree == String.Empty) S_Isroot = 0; trees = "1"; string treestr = tree + trees; DataRow[] dr = dt.Select("module_fatherid=http://www.likecs.com/\'" + moduleid + "\'"); for (int i = 0; i < dr.Length; i++) { NS_Name = dr[i]["module_name"].ToString(); NS_Id = dr[i]["module_id"].ToString(); string Ns_Parentid = dr[i]["module_fatherid"].ToString(); if (i + 2 > dr.Length) { trees = "0"; treestr = tree + trees; } BuilderBody.AppendLine(" <tr class=http://www.likecs.com/\'tdbg\' >"); //BuilderBody.AppendLine("<td vAlign=bottom width=http://www.likecs.com/\'5%\' align=http://www.likecs.com/\'center\'>" + NS_Id + "</td> "); BuilderBody.AppendLine("<td valign=http://www.likecs.com/\'bottom\' width=http://www.likecs.com/\'40%\'>"); #region 输出层级关系 for (int k = 0; k < treestr.Length - 1; k++) { if (treestr.Substring(k, 1) == "1") { BuilderBody.AppendLine("    "); } else { BuilderBody.AppendLine("    "); } } #endregion #region 判断是否无下级节点 DataRow[] dr1 = dt.Select("module_fatherid=http://www.likecs.com/\'" + NS_Id + "\'"); if (dr1.Length == 0) //无下级,即末级节点 { ParentBool = false; } else { ParentBool = true; } #endregion #region 输出树型图片 if (trees == "0") { if ((tree == "" || tree == String.Empty || tree == null) && S_Isroot == 0) { BuilderBody.AppendLine("    "); } else { BuilderBody.AppendLine("└"); } } else if (tree == "" || tree == String.Empty || tree == null) { if (S_Isroot == 1) { BuilderBody.AppendLine("├"); } else { BuilderBody.AppendLine("┌"); } } else { BuilderBody.AppendLine("├ "); } #endregion BuilderBody.AppendLine(NS_Name); BuilderBody.AppendLine("</td>"); BuilderBody.AppendLine("<td width=http://www.likecs.com/\'10%\'>"); BuilderBody.AppendLine(Ns_Parentid); BuilderBody.AppendLine("</td>"); if (ParentBool == false) { BuilderBody.AppendLine("<td width=http://www.likecs.com/\'40%\'>"); //查询该功能的所有操作 DataRow[] dtAction = dt.Select(" module_id=http://www.likecs.com/\'" + NS_Id + "\' "); if (dtAction .Length> 0) { for (int j = 0; j < dtAction.Length; j++) { BuilderBody.AppendLine("<input type=http://www.likecs.com/\'checkbox\' name=http://www.likecs.com/\'actionItem\' style=http://www.likecs.com/\'vertical-align:middle\' value=http://www.likecs.com/\'" + dtAction[j]["module_id"] + "\' />"); BuilderBody.AppendLine(dtAction[j]["module_name"].ToString() + " " + dtAction[j]["module_id"] + " "); } } BuilderBody.AppendLine("</td>"); BuilderBody.AppendLine("<td width=http://www.likecs.com/\'10%\'>"); BuilderBody.AppendLine(" <a href=http://www.likecs.com/\'javascript:showMyModalDialog(\"ModuleActionAdd.aspx?ModuleID=" + NS_Id + "&cmd=add\",\"600\",\"300\");\'>新增操作</a> "); BuilderBody.AppendLine("</td>"); } else { BuilderBody.AppendLine("<td width=http://www.likecs.com/\'40%\'>"); BuilderBody.AppendLine("</td>"); BuilderBody.AppendLine("<td width=http://www.likecs.com/\'10%\'>"); BuilderBody.AppendLine("</td>"); } BuilderBody.AppendLine("</tr>"); showtreeforline(dt, NS_Id, treestr); S_Isroot = 1; } } #endregion #region 创建数据 protected static DataTable createDT() { DataTable dt = new DataTable(); dt.Columns.Add("module_id"); dt.Columns.Add("module_name"); dt.Columns.Add("module_fatherid"); dt.Columns.Add("module_url"); dt.Columns.Add("module_order"); dt.Rows.Add("C1", "全国", "0", "", "1"); dt.Rows.Add("M01", "广东", "C1", "", "1"); dt.Rows.Add("M0101", "深圳", "M01", "", "100"); dt.Rows.Add("M010101", "南山区", "M0101", "", "1000"); dt.Rows.Add("M010102", "罗湖区", "M0101", "", "1001"); dt.Rows.Add("M010103", "福田区", "M0101", "", "1002"); dt.Rows.Add("M010104", "宝安区", "M0101", "", "1003"); dt.Rows.Add("M010105", "龙岗区", "M0101", "", "1004"); dt.Rows.Add("M01010301", "上梅林", "M010103", "", "1002001"); dt.Rows.Add("M01010302", "下梅林", "M010103", "", "1002002"); dt.Rows.Add("M01010303", "车公庙", "M010103", "", "1002003"); dt.Rows.Add("M01010304", "竹子林", "M010103", "", "1002004"); dt.Rows.Add("M01010305", "八卦岭", "M010103", "", "1002005"); dt.Rows.Add("M01010306", "华强北", "M010103", "", "1002006"); dt.Rows.Add("M0102", "广州", "M01", "", "101"); dt.Rows.Add("M010201", "越秀区", "M0102", "", "1105"); dt.Rows.Add("M010202", "海珠区", "M0102", "", "1106"); dt.Rows.Add("M010203", "天河区", "M0102", "", "1107"); dt.Rows.Add("M010204", "白云区", "M0102", "", "1108"); dt.Rows.Add("M010205", "黄埔区", "M0102", "", "1109"); dt.Rows.Add("M010206", "荔湾区", "M0102", "", "1110"); dt.Rows.Add("M010207", "罗岗区", "M0102", "", "1111"); dt.Rows.Add("M010208", "南沙区", "M0102", "", "1112"); return dt; } #endregion } #region DropDownList无限递归显示层次关系 /// <summary> /// 创建无限分级下拉列表框 /// </summary> /// <param>下拉控件</param> /// <param>源DataTable</param> /// <param>text字段</param> /// <param>value字段</param> /// <param>深度字段 例如parentid</param> public static void CreateLevelDropDown(DropDownList ddlst, DataTable dt, string text, string value, string parentid) { ArrayList allItems = new ArrayList(); DataRow[] rows = dt.Select(parentid + "=0"); foreach (DataRow row in rows) CreateLevelDropDownAssistant(dt, ref allItems, row, string.Empty, text, value, parentid); ListItem[] items = new ListItem[allItems.Count]; allItems.CopyTo(items); ddlst.Items.AddRange(items); } /// <summary> /// 递归绑定子节点 /// </summary> /// <param>源DataTable</param> /// <param>数组</param> /// <param>当前节点</param> /// <param>前缀</param> /// <param>text字段</param> /// <param>value字段</param> /// <param>深度字段 例如parentid</param> private static void CreateLevelDropDownAssistant(DataTable dt, ref ArrayList items, DataRow parentRow, string curHeader, string text, string value, string parentid) { ListItem newItem = new ListItem(curHeader + Until.CutString(parentRow[text].ToString(), 18, true), parentRow[value].ToString()); items.Add(newItem); DataRow[] rows = dt.Select(parentid + "=" + newItem.Value); for (int i = 0; i < rows.Length - 1; i++) CreateLevelDropDownAssistant(dt, ref items, rows[i], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┣", text, value, parentid); if (rows.Length > 0) CreateLevelDropDownAssistant(dt, ref items, rows[rows.Length - 1], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┗", text, value, parentid); } #endregion #region TreeView 无限递归显示层次关系目录树 /// <summary> /// 创建无限分级目录树TreeView /// </summary> /// <param>TreeView空间</param> /// <param>数据源DataTable</param> /// <param>text字段</param> /// <param>value字段</param> /// <param>深度字段 例如parentid</param> public static void CreateLevelTreeView(TreeView treeview, DataTable dt, string text, string value, string parentid) { DataView dv = dt.DefaultView; dv.RowFilter = parentid + "=0"; foreach (DataRowView drv in dv) { TreeNode node = new TreeNode(); node.Text = drv[text].ToString(); node.Value = drv[value].ToString(); node.Expanded = false; treeview.Nodes.Add(node); CreatTreeViewChildNode(dv, node, text, value, parentid); } } /// <summary> /// 递归绑定子节点 /// </summary> /// <param>源DataView</param> /// <param>当前节点</param> /// <param>text字段</param> /// <param>value字段</param> /// <param>深度字段 例如parentid</param> private static void CreatTreeViewChildNode(DataView dv, TreeNode parentNode, string text, string value, string parentid) { dv.RowFilter = parentid + "=" + parentNode.Value; foreach (DataRowView row in dv) { TreeNode replyNode = new TreeNode(); replyNode.Text = row[text].ToString(); replyNode.Value = row[value].ToString(); replyNode.Expanded = false; parentNode.ChildNodes.Add(replyNode); CreatTreeViewChildNode(dv, replyNode, text, value, parentid); } } /// <summary> /// 创建无限分级目录树TreeView /// </summary> /// <param>TreeView空间</param> /// <param>数据源DataTable</param> /// <param>text字段</param> /// <param>value字段</param> /// <param>url字段</param> /// <param>深度字段 例如parentid</param> public static void CreateLevelTreeView(TreeView treeview, DataTable dt, string text, string value, string url, string parentid) { DataView dv = dt.DefaultView; dv.RowFilter = parentid + "=0"; foreach (DataRowView drv in dv) { TreeNode node = new TreeNode(); node.Text = drv[text].ToString(); node.Value = drv[value].ToString(); node.NavigateUrl = drv[url].ToString(); node.Expanded = false; treeview.Nodes.Add(node); CreatTreeViewChildNode(dv, node, text, value, url, parentid); } } /// <summary> /// 递归绑定子节点 /// </summary> /// <param>源DataView</param> /// <param>当前节点</param> /// <param>text字段</param> /// <param>value字段</param> /// <param>url字段</param> /// <param>深度字段 例如parentid</param> private static void CreatTreeViewChildNode(DataView dv, TreeNode parentNode, string text, string value, string url, string parentid) { dv.RowFilter = parentid + "=" + parentNode.Value; foreach (DataRowView row in dv) { TreeNode replyNode = new TreeNode(); replyNode.Text = row[text].ToString(); replyNode.Value = row[value].ToString(); replyNode.NavigateUrl = row[url].ToString(); replyNode.Expanded = false; parentNode.ChildNodes.Add(replyNode); CreatTreeViewChildNode(dv, replyNode, text, value, url, parentid); } } #endregion #region 创建无限分级ListBox /// <summary> /// 创建无限分级ListBox /// </summary> /// <param>ListBox控件</param> /// <param>源DataTable</param> /// <param>text字段</param> /// <param>value字段</param> /// <param>深度字段 例如parentid</param> public static void CreateLevelListBox(ListBox ddlst, DataTable dt, string text, string value, string parentid) { ArrayList allItems = new ArrayList(); DataRow[] rows = dt.Select(parentid + "=0"); foreach (DataRow row in rows) CreateLevelListBoxAssistant(dt, ref allItems, row, string.Empty, text, value, parentid); ListItem[] items = new ListItem[allItems.Count]; allItems.CopyTo(items); ddlst.Items.AddRange(items); } /// <summary> /// 递归绑定子节点 /// </summary> /// <param>源DataTable</param> /// <param>数组</param> /// <param>当前节点</param> /// <param>前缀</param> /// <param>text字段</param> /// <param>value字段</param> /// <param>深度字段 例如parentid</param> private static void CreateLevelListBoxAssistant(DataTable dt, ref ArrayList items, DataRow parentRow, string curHeader, string text, string value, string parentid) { ListItem newItem = new ListItem(curHeader + Until.CutString(parentRow[text].ToString(), 18, true), parentRow[value].ToString()); items.Add(newItem); DataRow[] rows = dt.Select(parentid + "=" + newItem.Value); for (int i = 0; i < rows.Length - 1; i++) CreateLevelListBoxAssistant(dt, ref items, rows[i], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┣", text, value, parentid); if (rows.Length > 0) CreateLevelListBoxAssistant(dt, ref items, rows[rows.Length - 1], curHeader.Replace("┣", "┃").Replace("┗", "┣") + "┗", text, value, parentid); } #endregion

无限级递归生成HTML示例及ListBox,DropDownList等无限树

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zggwfg.html