[转] 一个可以添加无限结点的树(asp.net版)
1、递归函数
// '递归遍历自建目录树==================================
private void ShowDir(string sZml,string sIndex)
{
System.Data.SqlClient.SqlConnection sqlCon=new System.Data.SqlClient.SqlConnection(connectionString);
string sqlStr="select * from mltable where mlzml=N'"+ sZml +"'";
System.Data.SqlClient.SqlDataAdapter sqlAda=new System.Data.SqlClient.SqlDataAdapter(sqlStr,sqlCon);
System.Data.DataTable Dt=new DataTable();
sqlAda.Fill(Dt);
Microsoft.Web.UI.WebControls.TreeNode Node1;
string sIndex0;
string sIndex2;
int theId;
int sIndex1=0;//记录树的最后一位值
foreach(System.Data.DataRow DtDataRow in Dt.Rows)
{
theId=Convert.ToInt32(DtDataRow["mlid"]);
Node1=new Microsoft.Web.UI.WebControls.TreeNode();
Node1.Text=DtDataRow["mlmc"].ToString();
Node1.ImageUrl="images/trees/folder.gif";
Node1.ExpandedImageUrl="images/trees/folderopen.gif";
Node1.Expanded=false;
Node1.NavigateUrl="Admin_Tree_Show.aspx?theid=" + theId +"&sIndex="+ sIndex+"."+sIndex1.ToString();
Node1.ID="";
Node1.Target="main";
if(sIndex=="")
{
sIndex2=TreeView1.Nodes.IndexOf(Node1).ToString();
sIndex0=sIndex2;
}
else
{
Microsoft.Web.UI.WebControls.TreeNode pNode=TreeView1.GetNodeFromIndex(sIndex);
pNode.Nodes.Add(Node1);
sIndex2=pNode.Nodes.IndexOf(Node1).ToString();
sIndex0=sIndex + "." + sIndex2;
sIndex1++;//记录当前树下叶子的个数,即当前叶子的号
}
ShowDir(DtDataRow["mlid"].ToString(),sIndex0);
}
sqlCon.Close();
}
//=============================================
2、绑定目录树
// 绑定目录树=======================================
private void BindTree()
{
// TreeView1.SelectExpands=true;
Microsoft.Web.UI.WebControls.TreeNode Node1=new Microsoft.Web.UI.WebControls.TreeNode();
Node1.Text="管理中心";
Node1.ImageUrl="images/trees/folder.gif";
Node1.ExpandedImageUrl="images/trees/folderopen.gif";
Node1.ID="";
Node1.NavigateUrl="Admin_Glzx.aspx";
Node1.Target="main";
Node1.Expanded=true;
TreeView1.Nodes.Add(Node1);
//公司管理=======================================
Microsoft.Web.UI.WebControls.TreeNode Node2=new Microsoft.Web.UI.WebControls.TreeNode();
Node2.Text="公司管理";
Node2.ImageUrl="images/trees/folder.gif";
Node2.ExpandedImageUrl="images/trees/folderopen.gif";
Node2.ID="";
Node2.NavigateUrl="Admin_Gsgl.aspx?sIndex=0.0";
Node2.Target="main";
Node2.Expanded=false;
Node1.Nodes.Add(Node2);
Microsoft.Web.UI.WebControls.TreeNode Node21=new Microsoft.Web.UI.WebControls.TreeNode();
Node21.Text="公司简介";
Node21.ImageUrl="images/trees/folder.gif";
Node21.ExpandedImageUrl="images/trees/folderopen.gif";
Node21.ID="";
Node21.NavigateUrl="Admin_GsJj.aspx?sIndex=0.0.0";
Node21.Target="main";
Node21.Expanded=true;
Node2.Nodes.Add(Node21);
// Microsoft.Web.UI.WebControls.TreeNode Node22=new Microsoft.Web.UI.WebControls.TreeNode();
// Node22.Text="联系方式";
// Node22.ImageUrl="images/trees/folder.gif";
// Node22.ExpandedImageUrl="images/trees/folderopen.gif";
// Node22.ID="";
// Node22.NavigateUrl="Admin_Detail.htm";
// Node22.Target="main";
// Node22.Expanded=true;
// Node2.Nodes.Add(Node22);
//
Microsoft.Web.UI.WebControls.TreeNode Node23=new Microsoft.Web.UI.WebControls.TreeNode();
Node23.Text="公司新闻";
Node23.ImageUrl="images/trees/folder.gif";
Node23.ExpandedImageUrl="images/trees/folderopen.gif";
Node23.ID="";
Node23.NavigateUrl="Admin_GsXw.aspx?sIndex=0.0.1";
Node23.Target="main";
Node23.Expanded=true;
Node2.Nodes.Add(Node23);
Microsoft.Web.UI.WebControls.TreeNode Node24=new Microsoft.Web.UI.WebControls.TreeNode();
Node24.Text="公司公告";
Node24.ImageUrl="images/trees/folder.gif";
Node24.ExpandedImageUrl="images/trees/folderopen.gif";
Node24.ID="";
Node24.NavigateUrl="Admin_GsGg.aspx?sIndex=0.0.2";
Node24.Target="main";
Node24.Expanded=true;
Node2.Nodes.Add(Node24);
Microsoft.Web.UI.WebControls.TreeNode Node25=new Microsoft.Web.UI.WebControls.TreeNode();
Node25.Text="公司荣誉";
Node25.ImageUrl="images/trees/folder.gif";
Node25.ExpandedImageUrl="images/trees/folderopen.gif";
Node25.ID="";
Node25.NavigateUrl="Admin_GsRy.aspx?sIndex=0.0.3";
Node25.Target="main";
Node25.Expanded=true;
Node2.Nodes.Add(Node25);
Microsoft.Web.UI.WebControls.TreeNode Node26=new Microsoft.Web.UI.WebControls.TreeNode();
Node26.Text="营销网络";
Node26.ImageUrl="images/trees/folder.gif";
Node26.ExpandedImageUrl="images/trees/folderopen.gif";
Node26.ID="";
Node26.NavigateUrl="Admin_GsYx.aspx?sIndex=0.0.4";
Node26.Target="main";
Node26.Expanded=true;
Node2.Nodes.Add(Node26);
Microsoft.Web.UI.WebControls.TreeNode Node27=new Microsoft.Web.UI.WebControls.TreeNode();
Node27.Text="公司形象";
Node27.ImageUrl="images/trees/folder.gif";
Node27.ExpandedImageUrl="images/trees/folderopen.gif";
Node27.ID="";
Node27.NavigateUrl="Admin_GsXx.aspx?sIndex=0.0.5";
Node27.Target="main";
Node27.Expanded=true;
Node2.Nodes.Add(Node27);
Microsoft.Web.UI.WebControls.TreeNode Node28=new Microsoft.Web.UI.WebControls.TreeNode();
Node28.Text="下载专区";
Node28.ImageUrl="images/trees/folder.gif";
Node28.ExpandedImageUrl="images/trees/folderopen.gif";
Node28.ID="";
Node28.NavigateUrl="Admin_Xzzq.aspx?sIndex=0.0.6";
Node28.Target="main";
Node28.Expanded=true;
Node2.Nodes.Add(Node28);
//============================================================
//产品管理========================================================
string sIndex1;
string sIndex2;
Microsoft.Web.UI.WebControls.TreeNode Node3=new Microsoft.Web.UI.WebControls.TreeNode();
Node3.Text="产品管理";
Node3.ImageUrl="images/trees/folder.gif";
Node3.ExpandedImageUrl="images/trees/folderopen.gif";
Node3.ID="";
Node3.NavigateUrl="Admin_Tree_Show.aspx?theid=0&sIndex=0.1";
Node3.Target="main";
Node3.Expanded=true;
Node1.Nodes.Add(Node3);
sIndex1=TreeView1.Nodes.IndexOf(Node1).ToString();//根目录
sIndex2=Node1.Nodes.IndexOf(Node3).ToString();//产品管理目录
ShowDir("0",sIndex1+"."+sIndex2);
}
//=============================================================
3、数据表格式
3 mlid int 4 0
0 mlmc nvarchar 50 1
0 mlzml nvarchar 50 1
0 mllx int 4 1
0 thedate datetime 8 1