asp.net|treeview| Data | database Programming Ideas: each record represents a node. The logical hierarchical relationship is formed by the F_nodeidandF_parentnodeid of the 2 Digital core fields in the table. The contract 0 represents the root node. Other fields are secondary fields and are not related to this article. define and initialize an object array mynodes, and then read the records in the table at once, establishing all the subordinate relationships by determining the parent node of the current node. Sample Data: " Run Result: Sample Downloads:Http://www.why100000.com/_ftps/samples/Tree+db.rar (Code Debugging Environment:Visual Web Developer, Edition Express) Access database name:db_system.mdb table name:tabitems table structure: f_i_autoid automatic numbering F_nodeid Number (this node ID) F_parentnodeid Number (parent node ID) f_name text f_tag text f_url text f_order Digital f_ishidden text f_datetime Date / time
Key code: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.MapPa Th (sDBPath) + "; Password= "+ Spassword +"; ");OleDbDataReader oDr;
Try { oConn.Open (); OleDbCommand ocmd = new OleDbCommand ("SELECT * from" + sdbtable, oconn); //+ "ORDER by F" _level " oDr = Ocmd.executereader ();
// Define Object array treenode[] mynodes = new treenode[100];
// Initialize array of objects for (int i = 0; i < mynodes.length i++) { mynodes[ I] = new TreeNode (); }
// fetch data from the tableWhile (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 (); }
}
}}