数据库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 ); //递归
}
}