天天看點

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

 假設在資料庫中建立一張表, 如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>