[ASP.net]TreeView(1)(一次性遞迴所有節點)

來源:互聯網
上載者:User

資料庫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 );  //遞迴
   }
  }

=================================================

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.