資料庫Table
==============================================
int key int nvarchar(100) nvarchar(100) nvarchar(100) nvarchar(100)
NODE_ID PARENT_ID NODE_NAME ADDRESS IMAGE_URL SELECT_IMAGE_URL
1 0 node_1 http://* *.gif *.gif
2 1 node_1_1 http://* *.gif *.gif
3 2 node_1_1_1 http://* *.gif *.gif
4 0 node_2 http://* *.gif *.gif
5 4 node_2_1 http://* *.gif *.gif
6 5 node_2_1_1 http://* *.gif *.gif
7 0 node_3 http://* *gif *.gif
8 7 node_3_1 http://* *.gif *.gif
==============================================
代碼程式:
==============================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Components;
using Microsoft.Web.UI.WebControls;
namespace Register
{
/// <summary>
/// WebForm1 的摘要說明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView tvList;
private SqlControl objSqlControl;
private DataTable objDataTable;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面
if ( ! Page.IsPostBack )
{
objSqlControl = new SqlControl();
TreeNode node = new TreeNode();
objDataTable = objSqlControl.SelectData(); //取得所有資料得到DataTable
this.CreateTree( tvList.Nodes,"0" ); //建立節點
}
}
private void CreateTree( TreeNodeCollection node,string parent_id )
{
DataView dvList = new DataView( this.objDataTable );
dvList.RowFilter = "PARENT_ID='" + parent_id + "'"; //過濾父節點
TreeNode nodeTemp;
foreach ( DataRowView dv in dvList )
{
nodeTemp = new TreeNode();
nodeTemp.ID = dv["NODE_ID"].ToString(); //節點ID
nodeTemp.Text = dv["NODE_NAME"].ToString(); //節點名稱
nodeTemp.NavigateUrl = dv["ADDRESS"].ToString(); //節點連結地址
nodeTemp.ImageUrl = dv["IMAGE"].ToString(); //節點圖片(未展開)
nodeTemp.SelectedImageUrl = dv["IMAGE_EX"].ToString(); //節點圖片(展開)
nodeTemp.Target = "_parent"; //節點連結Target
node.Add( nodeTemp ); //加入節點
this.CreateTree( nodeTemp.Nodes,nodeTemp.ID ); //遞迴
}
}
=================================================