ASP.NET 2.0 中實現 Treeview 與資料庫的綁定(含代碼)

來源:互聯網
上載者:User
asp.net|treeview|資料|資料庫 編程思想:   每條記錄代表一個節點。通過表裡的 2 個數字型的核心欄位 f_NodeId、f_ParentNodeId 形成邏輯上的層次型關係。約定 0 代表根節點。其他欄位為輔助欄位,與本文關係不大。定義和初始化一對象數組 myNodes,然後一次性讀取表中的記錄,通過判斷當前節點的父節點,依次建立所有的上下級關係。樣本資料:運行結果:樣本下載:http://www.why100000.com/_ftps/samples/Tree+db.rar(代碼調試環境:Visual Web Developer 2005 Express Edition)Access資料庫名:db_system.mdb表名:tabItems表結構:    f_i_autoid      自動編號    f_NodeId        數字(本節點ID)    f_ParentNodeId  數字(父節點ID)    f_Name          文本    f_Tag           文本    f_Url           文本    f_order         數字    f_isHidden      文本    f_datetime      日期/時間

  關鍵代碼:using System;using System.Data;using System.Data.OleDb;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;

  public partial class _Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            string sDbPath = "./db_system.mdb";  //"            string sPassword = "";            string sDbTable = "tabItems";            OleDbConnection oConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(sDbPath) + ";Password=" + sPassword + ";");            OleDbDataReader oDr;

              try            {                oConn.Open();                OleDbCommand oCmd = new OleDbCommand("select * from " + sDbTable, oConn);  // + " order by f_level"                oDr = oCmd.ExecuteReader();

                  //定義對象數組                TreeNode[] myNodes = new TreeNode[100];

                  //初始化對象數組                for (int i = 0; i < myNodes.Length; i++)                {                    myNodes[i] = new TreeNode();                }

                  //從表中取資料                while (oDr.Read())                {                    int iParentNodeId = (int)oDr["f_ParentNodeId"];                    int iMyNodeId = (int)oDr["f_NodeId"];

                      if (iParentNodeId != 0)                    {                        myNodes[iMyNodeId].Text = oDr["f_name"].ToString();                        myNodes[iMyNodeId].NavigateUrl = oDr["f_url"].ToString();                        myNodes[iParentNodeId].ChildNodes.Add(myNodes[iMyNodeId]);                    }                    else                    {                        myNodes[iMyNodeId].Text = oDr["f_name"].ToString();                        myNodes[iMyNodeId].SelectAction = TreeNodeSelectAction.None;                        TreeView1.Nodes.Add(myNodes[iMyNodeId]);                    }                }            }

              catch (System.Exception sqle)            {                sqle.ToString().Replace("\n", "<br>");                Response.Write(sqle);            }            finally            {                oConn.Close();            }

          }

      }}

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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