文档章节

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

深圳大道
 深圳大道
发布于 2016/12/29 15:37
字数 1716
阅读 34
收藏 0
<%@ 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 id="form1" runat="server">
    <div>
        <asp:Literal ID="LiteralTree" 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='tableAction' class='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='" + 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='tdbg' >");
            //BuilderBody.AppendLine("<td vAlign=bottom width='5%' align='center'>" + NS_Id + "</td> ");
            BuilderBody.AppendLine("<td valign='bottom' width='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='" + 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='10%'>");
            BuilderBody.AppendLine(Ns_Parentid);
            BuilderBody.AppendLine("</td>");

            if (ParentBool == false)
            {
                BuilderBody.AppendLine("<td width='40%'>");
                //查询该功能的所有操作
                DataRow[] dtAction = dt.Select(" module_id='" + NS_Id + "' ");
                if (dtAction .Length> 0)
                {
                    for (int j = 0; j < dtAction.Length; j++)
                    {
                        BuilderBody.AppendLine("<input type='checkbox' name='actionItem' style='vertical-align:middle' value='" + dtAction[j]["module_id"] + "' />");
                        BuilderBody.AppendLine(dtAction[j]["module_name"].ToString() + " " + dtAction[j]["module_id"] + " ");
                    }
                }
                BuilderBody.AppendLine("</td>");
                BuilderBody.AppendLine("<td width='10%'>");
                BuilderBody.AppendLine(" <a href='javascript:showMyModalDialog(\"ModuleActionAdd.aspx?ModuleID=" + NS_Id + "&cmd=add\",\"600\",\"300\");'>新增操作</a> ");
                BuilderBody.AppendLine("</td>");
            }
            else
            {
                BuilderBody.AppendLine("<td width='40%'>");
                BuilderBody.AppendLine("</td>");
                BuilderBody.AppendLine("<td width='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 name="ddlst">下拉控件</param>
        /// <param name="dt">源DataTable</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如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 name="dt">源DataTable</param>
        /// <param name="items">数组</param>
        /// <param name="parentRow">当前节点</param>
        /// <param name="curHeader">前缀</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如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 name="treeview">TreeView空间</param>
        /// <param name="dt">数据源DataTable</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如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 name="dv">源DataView</param>
        /// <param name="parentNode">当前节点</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如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 name="treeview">TreeView空间</param>
        /// <param name="dt">数据源DataTable</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="url">url字段</param>
        /// <param name="parentid">深度字段 例如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 name="dv">源DataView</param>
        /// <param name="parentNode">当前节点</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="url">url字段</param>
        /// <param name="parentid">深度字段 例如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 name="ddlst">ListBox控件</param>
        /// <param name="dt">源DataTable</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如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 name="dt">源DataTable</param>
        /// <param name="items">数组</param>
        /// <param name="parentRow">当前节点</param>
        /// <param name="curHeader">前缀</param>
        /// <param name="text">text字段</param>
        /// <param name="value">value字段</param>
        /// <param name="parentid">深度字段 例如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

 

本文转载自:http://blog.csdn.net/smartsmile2012/article/details/7899989

深圳大道
粉丝 3
博文 877
码字总数 0
作品 0
深圳
架构师
私信 提问
在GridView内访问特定控件

本文我将为你演示如何访问GridView中的特定控件。我们会看到怎样去访问TextBox控件,DropDownList控件以及ListBox控件。 添加控件到GridView: 你可以简单地使用 选项在GridView控件中增加不...

晨曦之光
2012/03/09
102
0
ASP.NET MVC的Model元数据与Model模板:将”ListControl”引入ASP.NET MVC

我们不仅可以创建相应的模板来根据Model元数据控制种类型的数据在UI界面上的呈现方法,还可以通过一些扩展来控制Model元数据本身。在某些情况下通过这两者的结合往往可以解决很多特殊数据的呈...

长平狐
2012/09/04
1K
0
DataBind数据核心

作者:飞刀 这一节主要是要讲DataBind,这个在ASP.net中是很重要的东东,几乎所有的控件都需要它来控制数据的操作。也可以说是ASP.net的数据核心。 我们先来看一个简单的例子: <% @ Page La...

晨曦之光
2012/03/09
75
0
WEB控件

作者:飞刀 WEB控件 我想了半天也不知道如何定义这个web控件,它比html控件难于理解,毕竟HTML太像原来我们熟悉的东东了。而web控件确与我们平常见到的有很大的出入,不知道大家还记不记得我...

晨曦之光
2012/03/09
427
0
DropDownList问题,进来看!

有个表格,第一个tr有DropDownList,是为了修改下面全部DropDownList的,下面的tr也有DropDownList,最左边是checkbox ,现在想选中最左边的几项checkbox,修改第一个tr的DropDownList,相应下...

钟颖
2012/11/19
109
2

没有更多内容

加载失败,请刷新页面

加载更多

Leetcode PHP题解--D118 350. Intersection of Two Arrays II

D118 350. Intersection of Two Arrays II 题目链接 350. Intersection of Two Arrays II 题目分析 返回给定两个数组的交集。 思路 从数量较多的那个数组开始去另一个数组寻找是否元素存在,...

skys215
18分钟前
2
0
从源码上分析Android View保存数据状态

在Android开发旅途中,经常会遇到系统控件无法满足我们的视觉,交互效果,这个时候我们常常需要自己自定义控件来满足我们的需求。在这个开发探索过程中,我们不可避免得遇到View要保存状态信...

shzwork
19分钟前
2
0
请问AD603AQ和AD603AR有什么区别?

  AD603AQ和AD603AR只是在封装上的区别,前者是双列直插式,后者是贴片式,AD603A系列的温度都是在—40摄氏度到+85摄氏度之间,AD603还有一个系列是AD603S,它的温度是在—55摄氏度到+125摄...

仙溪
21分钟前
2
0
Linux /etc/profile 配置文件修改

1. 执行命令: vi /etc/profile 去类似windows 配置环境变量, 2.修改完,立即生效命令: source /etc/profile

kuchawyz
22分钟前
3
0
对于小白来说素描怎么入门?怎么学习?

素描初学者怎样入门?初学者怎样才能画好素描绘画?画好素描绘画有哪些技巧?想必这些问题都是绘画初学者们比较伤脑筋的问题,那么初学者到底怎样才能画好素描绘画呢?今天收集整理了关于素描...

huihuajiaocheng
23分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部