C# 递归无限级树型菜单

程序代码 程序代码

        protected override void ShowPage()
        {
            DataTable workTable = new DataTable("Customers");
            DataColumn workCol = workTable.Columns.Add("sortID", typeof(Int32));
            workCol.AllowDBNull = false;
            workCol.AutoIncrement = true;
            workCol.AutoIncrementSeed = 1;
            workCol.AutoIncrementStep = 1;
            workCol.Unique = true;
            workTable.Columns.Add("sortName", typeof(String));
            workTable.Columns.Add("parentID", typeof(Int32));
            workTable.Columns.Add("level", typeof(Int32));
            workTable.Columns.Add("sortPath", typeof(String));

            DataRow newRow = workTable.NewRow();

            newRow["sortName"] = "国产手机";
            newRow["parentID"] = "0";
            newRow["level"] = "0";
            newRow["sortPath"] = ",1,";
            workTable.Rows.Add(newRow);

            newRow = workTable.NewRow();
            newRow["sortName"] = "国际手机";
            newRow["parentID"] = "0";
            newRow["level"] = "0";
            newRow["sortPath"] = ",2,";
            workTable.Rows.Add(newRow);

            newRow = workTable.NewRow();
            newRow["sortName"] = "诺基亚";
            newRow["parentID"] = "1";
            newRow["level"] = "1";
            newRow["sortPath"] = ",1,";
            workTable.Rows.Add(newRow);

            newRow = workTable.NewRow();
            newRow["sortName"] = "金立";
            newRow["parentID"] = "2";
            newRow["level"] = "1";
            newRow["sortPath"] = ",2,";
            workTable.Rows.Add(newRow);

            newRow = workTable.NewRow();
            newRow["sortName"] = "5800XM";
            newRow["parentID"] = "3";
            newRow["level"] = "2";
            newRow["sortPath"] = ",1,3,";
            workTable.Rows.Add(newRow);

            newRow = workTable.NewRow();
            newRow["sortName"] = "6100S";
            newRow["parentID"] = "3";
            newRow["level"] = "2";
            newRow["sortPath"] = ",1,3,";
            workTable.Rows.Add(newRow);

            newRow = workTable.NewRow();
            newRow["sortName"] = "A4";
            newRow["parentID"] = "4";
            newRow["level"] = "2";
            newRow["sortPath"] = ",2,4,";
            workTable.Rows.Add(newRow);

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            DataRow[] rows = workTable.Select("parentID=0");
            foreach (DataRow dr in rows)
            {

                sb.AppendLine("<div>" + dr["sortName"] + "(" + dr["sortPath"].ToString() + ")");
                sb.AppendLine(getSubNode(dr["sortID"].ToString(), workTable));
                sb.AppendLine("</div>");
            }
            Response.Write(sb.ToString());
        }

        private string getSubNode(string sortID, DataTable ATable)
        {
            System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
            DataRow[] rows = ATable.Select("[parentID]=" + sortID);
            foreach (DataRow dr in rows)
            {
                sb2.AppendLine("<li>" + dr["sortName"] + "(" + dr["sortPath"].ToString() + ")</li>");
                sb2.AppendLine(getSubNode(dr["sortID"].ToString(), ATable));
            }
            return sb2.ToString();
        }

[本日志由 MONO 于 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: C# 递归 无限级 树型菜单
评论: 0 | 引用: 0 | 查看次数: 1249
发表评论
你没有权限发表评论!