asp.net 2.0 to implement the Treeview binding with the database (with code)

Source: Internet
Author: User
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 ();            }

         }

     }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.