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();
}