我使用的是递归是实现无限级树形菜单:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace Power.Sbtree
{
    public partial class Left : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            showTree();
        }

        private void addChildNode(TreeNode parent, string parentID)
        {
            //获得以parent的ID为父节点的子结点
            DataTable dtChild = SqlHelper.ExecSQLDataTable("select * from tab_Sbtree where Sb_parentID='" + parentID + "'");
            if (dtChild.Rows.Count > 0)
            {
                for (int i = 0; i < dtChild.Rows.Count; i++)
                {
                    //TreeNode node1 = parent.ChildNodes.Add(dtChild.Rows[i][0].ToString().Trim(), dtChild.Rows[i][1].ToString().Trim(), 1);

                    TreeNode node1 = new TreeNode();
                    node1.Text = dtChild.Rows[i][1].ToString().Trim();
                    parent.ChildNodes.Add(node1);

                    addChildNode(node1, dtChild.Rows[i][0].ToString().Trim());
                }
            }
        }
        private void showTree()
        {
            try
            {
                treeView1.Nodes.Clear();
                DataTable dtEquipment = SqlHelper.ExecSQLDataTable("select * from tab_Sbtree where Sb_parentID='0'");  //读总根结点

                for(int i =0;i<dtEquipment.Rows.Count;i++)
                {
                   TreeNode node0 = new TreeNode();
                   node0.Text= dtEquipment.Rows[i][1].ToString().Trim();
                   treeView1.Nodes.Add(node0);//根节点

                   addChildNode(node0, dtEquipment.Rows[i][0].ToString().Trim());
                }

            }
            catch (SqlException ex)
            {
                //MessageBox.Show("数据库访问异常" + ex.Message.ToString(), "错误提示框", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Response.Write("<Script Language=JavaScript>alert('数据库访问异常!')</Script>");
            }
        }

        protected void treeView1_SelectedNodeChanged(object sender, EventArgs e)
        {

        }

        protected void treeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
        //    Session["tree"]=treeView1.test;
        }

    }
}

  我这借助的使用了sqlhelper 这个工具类,效果如下图

treeview树形菜单,递归-LMLPHP

下面是我的数据库

treeview树形菜单,递归-LMLPHP

这就是一个简单的树形菜单。

05-11 15:17