Asp.net树形递归算法

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 WebApp
{
    public partial class LoadMenu : System.Web.UI.Page
    {
        DataSet ds = null;

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //this.InitPage();

ds = GetDB("select * from page");
                TreeNode pTreeNpd = null;
                GetTreeNode("0",pTreeNpd);
            }
        }

private void InitPage()
        {
            TreeNode treeNode = this.GetTreeNode();
            this.TreeViewMenu.Nodes.Add(treeNode);
        }

private TreeNode GetTreeNode()
        {
            TreeNode treeNode = new TreeNode("菜单导航");
            DataSet ds = this.GetDB("select * from page where Parent = '0'");
            foreach (DataRow masterRow in ds.Tables[0].Rows)
            {
                    TreeNode masterNode = new TreeNode((string)masterRow["Text"]);
                    masterNode.NavigateUrl = (string)masterRow["Page"];
                    treeNode.ChildNodes.Add(masterNode);

DataSet ds11 = this.GetDB("select * from page where Parent = '" + masterRow["RID"] + "'");
                    foreach (DataRow item in ds11.Tables[0].Rows)
                    {
                        TreeNode masterNode1 = new TreeNode((string)item["Text"]);
                        masterNode1.NavigateUrl = (string)item["Page"];
                        masterNode.ChildNodes.Add(masterNode1);
                    }

}
            return treeNode;
        }

private TreeNode GetTreeNode(string Parent, TreeNode treeNode)
        {
            DataView dv = ds.Tables[0].DefaultView;
            dv.RowFilter = " [Parent]='" + Parent + "'";
            foreach (DataRowView item in dv)
            {
                TreeNode tttrr = new TreeNode();
                if (treeNode == null)
                {
                    tttrr.Text = item["Text"].ToString();
                    TreeViewMenu.Nodes.Add(tttrr);

GetTreeNode(item["rid"].ToString(), tttrr);
                }
                else
                {
                    tttrr.Text = item["Text"].ToString();
                    tttrr.NavigateUrl = (string)item["Page"];
                    treeNode.ChildNodes.Add(tttrr);
                }

}
            return null;
        }

private DataSet GetDB(string sql)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = “";
            if(con.State == ConnectionState.Closed)
            {
                con.Open();
            }

SqlDataAdapter da = new SqlDataAdapter(sql, con);

DataSet ds = new DataSet();
            da.Fill(ds);

return ds;
        }
    }
}

上一篇:地产cio揭秘:帆软大商业智能解决方案如何助力地产行业信息化


下一篇:C# 面向对象之概念理解(3)