table :
id parent_id name value
function:
getSubNodes(long pParentID)// 根据pParentID的值返回所有下级节点在程序里使用递归来得到所有的节点
比如:顶级节点的Parent_id=-1,而其他的都是对应父节点的ID
id parent_id name value
function:
getSubNodes(long pParentID)// 根据pParentID的值返回所有下级节点在程序里使用递归来得到所有的节点
比如:顶级节点的Parent_id=-1,而其他的都是对应父节点的ID
id as integer
node as integer
text as string
child as new collection
end class在用一个循环加递归就行了
数据库设计最好改一下能合理一些
menuid menuanme (parentid)上级id
01 *** null
0101 *** 01
010101 ** 0101
这样就比较好一点
public class tree : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView TreeView1; public string strsql;
public string strConn;
public DataSet ds = new DataSet() ;
public DataSet ds1 = new DataSet() ; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
TreeView1.Nodes.Clear();
CreateDataSet();
intiTree(TreeView1.Nodes, "");
//InitDevelop();
}
}
private void CreateDataSet()
{
SqlConnection myConn =new SqlConnection();
strConn =System.Configuration.ConfigurationSettings.AppSettings["strconn"]; SqlCommand myCmd =new SqlCommand("select * from SY_Menu",myConn);
SqlDataAdapter datmenu1 =new SqlDataAdapter();
myConn.ConnectionString = strConn;
myCmd.Connection = myConn;
datmenu1.SelectCommand = myCmd;
datmenu1.Fill(ds, "menu1"); }
private void intiTree(TreeNodeCollection Nds,string parentId)
{
//int intId ;
string strid;
DataView dv =new DataView();
dv.Table = ds.Tables["menu1"];
TreeNode tmpNd ; dv.RowFilter = "parentid='" + parentId + "'";
foreach(DataRowView drv in dv)
{
tmpNd = new TreeNode();
strid = drv["menuid"].ToString();
tmpNd.ID = strid;
//tmpNd.Expanded = true;
tmpNd.Expanded=false;
tmpNd.Text = drv["menuname"].ToString() ;
if(drv["menulevel"].ToString() != null)
{ tmpNd.NavigateUrl=drv["ule"].ToString();
tmpNd.Target = "main";
}
Nds.Add(tmpNd);
intiTree(Nds[Nds.Count - 1].Nodes, strid);
}
}