asp.net TreeView動態綁定資料庫顯示資料

來源:互聯網
上載者:User

 假設在資料庫中建立一張表, 如tb_treeview,如下建立

         create table tb_treeview(

                    id  int not null,

                    displayName varchar(20),

                    parentId int ,

                    url varchar(100)

            );

 

向表中插入幾個資料如:

       insert into tb_treeview(1,'中國',0,'');

       insert into tb_treeview(2,'安徽省',1,'');

       insert into tb_treeview(3,'合肥市',2,'');

       insert into tb_treeview(4,'江蘇省',1,'');

       insert into tb_treeview(5,'南京市',4,'');

       insert into tb_treeview(6,'湖北省',1,'');

 

作為示範資料。

(1)、開啟VS2005,建立一個網站項目,在Default.aspx.cs檔案裡添加如下代碼:

...

   using System.Data.SqlClient;

  ....

   DataTable dt = new DataTable();

   public void Page_Load(object sender,EventArgs e)

   {

        if(!IsPostBack)

        {

               try

               {

                      string conString = "Data Source=.;uid=sa;pwd=;Database=master";

                      SqlConnection con = new SqlConnection(conString);

                      con.Open();

                      string strSQL = "select * from tb_treeview";

                      SqlDataAdapter da = new SqlDataAdapter(strSQL,con);

                      da.Fill(dt);

               }

               catch(Exception e)

               {

 

               }

               finally

               {

                     con.Close();

               }

               AddTreeNode(0,(TreeNode)null);

        }

   }

 

   protected void AddTreeNode(int parentId,TreeNode pNode)

   {

            TreeNode tn1 = new TreeNode(); 

            DataView dv= new DataView(dt);

            //過濾parentId,得到當前節點的所有子節點

            dv.RowFilter = "parentId="+parentId;

             foreach(DataRowView drv in dv)

             {

                   if(pNode==null)

                   {

                          tn1.Text = drv["displayName"].ToString(); //節點上要顯示的名稱

                          tn1.NavigateUrl = drv["url"].ToString();  //點擊節點名稱,跳轉到指定url頁面

                          TreeView1.Nodes.Add(tn1); //將根節點加入到TreeView中去

                          tn1.Expanded = true;

                          //遞迴調用

                          AddTreeNode(Int32.parse(drv["id"].ToString()),tn1);

                   }

                   else

                   {

                          TreeNode tn2 = new TreeNode();

                           tn2.Text = drv["displayName"].ToString();

                           tn2.NavigateUrl = drv["url"].ToString();

                           pNode.ChildNodes.Add(tn2);

                           tn1.Expanded = true;

                           //遞迴調用

                           AddTreeNode(Int32.parse(drv["id"].ToString()),tn2);

                   }

             }

   }

 

(2)、在瀏覽器中運行,開啟效果如下

            +中國

             |+安徽省

             |  |_合肥市

             |+江蘇省

             |   |_南京市

             |+湖北省

需要說明的是,在實際開發中,關於資料庫的資料訪問應該放在DAL層裡,這裡只是做簡單的示範而已。

如果想改變已經選擇的節點,和移動到該節點時字型的呈現顏色的話,可以這樣設定:

<asp:TreeView ID="TreeView1" runat="server" ShowLines="True" CssClass="Menu">
            <RootNodeStyle  ForeColor="red"/>
            <SelectedNodeStyle ForeColor="gray" />
            <HoverNodeStyle ForeColor="aqua" />

</asp:TreeView>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.