using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.OleDb;public partial class _Default : System.Web.UI.Page {private OleDbConnection connection = null;public OleDbCommand command = null;public string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Inetpub\\wwwroot\\Test\\Menu3\\menu.mdb;";protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){fill_Tree();}}void fill_Tree(){/** Fill the treeview control Root Nodes From Parent Table* and child nodes from ChildTables*/connection = new OleDbConnection(connectionString); connection.Open();/** Query the database*/command = new OleDbCommand();command.Connection = connection;DataTable myDataTable = new DataTable();myDataTable.Columns.Add(new DataColumn("CategoryID", Type.GetType("System.String")));myDataTable.Columns.Add(new DataColumn("ParentCategoryID", Type.GetType("System.String")));myDataTable.Columns.Add(new DataColumn("CategoryName", Type.GetType("System.String")));command.CommandText = "SELECT * FROM CATEGORIES WHERE ParentCategoryID = 0";command.Parameters.Clear();/**Define and Populate the SQL DataReader*/OleDbDataReader myReader = command.ExecuteReader();/** Dispose the SQL Command to release resources*/command.Dispose();/** Initialize the string ParentNode.* We are going to populate this string array with our ParentTable Records* and then we will use this string array to populate our TreeView1 Control with parent records*/string[,] ParentNode = new string[100, 2];/** Initialize an int variable from string array index*/int count = 0;/** Now populate the string array using our SQL Datareader Sdr.*/while (myReader.Read()){ParentNode[count, 0] = myReader.GetValue(myReader.GetOrdinal("CategoryID")).ToString();ParentNode[count++, 1] = myReader.GetValue(myReader.GetOrdinal("CategoryName")).ToString();}/** Close the SQL datareader to release resources*///myReader.Close();/** Now once the array is filled with [Parentid,ParentName]* start a loop to find its child module.* We will use the same [count] variable to loop through ChildTable* to find out the number of child associated with ParentTable.*/for (int loop = 0; loop < count; loop++){/** First create a TreeView1 node with ParentName and than* add ChildName to that node*/TreeNode root = new TreeNode();root.Text = ParentNode[loop, 1];root.PopulateOnDemand = false;root.SelectAction = TreeNodeSelectAction.SelectExpand;//root.Target = "_self";/** Give the url of your page*/root.NavigateUrl = "Default.aspx?id=" + ParentNode[loop, 0];myReader.Close();/** Now that we have [ParentId] in our array we can find out child modules*/OleDbCommand Module_SqlCmd = new OleDbCommand("Select * from CATEGORIES where ParentCategoryID =" + ParentNode[loop, 0], connection);OleDbDataReader Module_Sdr = Module_SqlCmd.ExecuteReader();while (Module_Sdr.Read()){// Add children module to the root nodeTreeNode child = new TreeNode();child.Text = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("CategoryName")).ToString();child.Target = "_blank";child.NavigateUrl = "your_page_Url.aspx";root.ChildNodes.Add(child);}Module_Sdr.Close();// Add root node to TreeViewTreeView1.Nodes.Add(root);}/** By Default, when you populate TreeView Control programmatically, it expends all nodes.*/TreeView1.CollapseAll();connection.Close();}protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e){TreeView1.CollapseAll();ExpandNodes(TreeView1.SelectedNode.ValuePath);}private void ExpandNodes(string valuepath){string[] tmp = valuepath.Split('/');string tmpValuePath = string.Empty;foreach (string s in tmp){tmpValuePath += s;TreeView1.FindNode(tmpValuePath).Expand();tmpValuePath += "/";}}}