TreeView(一次性递归所有节点)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值