日期:2014-05-18  浏览次数:20456 次

[分享]分享一个自己写的理论上支持无限级分类的树型机构
.aspx页面
C# code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>myTree</title>
    <style type="text/css">
    <!--
        ul{margin:0px;padding:4px 0px 4px 25px;list-style:none;}
        li,span,div{font-size:12px;}
    //-->
    </style>
</head>
<body>
<asp:Literal ID="lit_Tree" runat="server"></asp:Literal>
<script language="javascript" type="text/javascript">
<!--
    function Change(id) {
        var el = document.getElementById(id);
        if(el.style.display == "none") {
            el.style.display = "";
            document.getElementById("img_" + id).innerHTML = "-";
        } else {
            el.style.display = "none";
            document.getElementById("img_" + id).innerHTML = "+";
        }
    }
//-->
</script>
</body>
</html>


.cs

C# code
public partial class index : System.Web.UI.Page
    {
        private DBHelper db = new DBHelper();

        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb_tree = new StringBuilder();
            if (Cache["Tree"] == null)
                CacheTree();
            GetTree(0, sb_tree);
            lit_Tree.Text = sb_tree.ToString();
        }
        protected void GetTree(int parentID, StringBuilder sb_tree)
        {
            DataTable dt_all = Cache["Tree"] as DataTable;
            string typeName = GetTypeName(parentID); 
            sb_tree.Append(string.Format("<span onClick=\"Change('{0}');\" style=\"cursor:pointer;\"><span id='img_{0}'>+</span> {1}</span>", parentID, typeName));
            DataRow[] dr_Item = dt_all.Select(string.Format("typeParentID={0}", parentID));
            if (parentID == 0)
                sb_tree.Append(string.Format("<div id='{0}' style='display:;'>\r\n", parentID));
            else
                sb_tree.Append(string.Format("<div id='{0}' style='display:none;'>\r\n", parentID));
            foreach(DataRow dr in dr_Item)
            {
                sb_tree.Append("<ul>\r\n");
                if (HaveChild(int.Parse(dr["typeID"].ToString())))
                    GetTree(int.Parse(dr["typeID"].ToString()), sb_tree); //===>重要!递归!
                else
                    sb_tree.Append(string.Format("<li>·{0}</li>\r\n", dr["typeName"].ToString()));
                sb_tree.Append("</ul>\r\n");
            }
            sb_tree.Append("</div>\r\n");
        }

        //判断该节点是否有子节点
        protected bool HaveChild(int myID)
        {
            DataTable dt_all = Cache["Tree"] as DataTable;
            return dt_all.Select(string.Format("typeParentID={0}", myID)).Length > 0;
        }

        //缓存该树的所有节点
        protected void CacheTree()
        {
            string cmdText = "select * from type_trade";
            DataTable dt = db.ExecuteDataTable(cmdText);
            Cache.Insert("Tree", dt);
            dt = null;
        }

        //根据节点取该节点的名称
        protected string GetTypeName(int typeID)
        {
            if (typeID == 0)
                return "根节点";
            if (Cache["Tree"] == null)
                CacheTree();
            DataTable dt_all = Cache["Tree"] as DataTable;
            return dt_all.Select(string.Format(&