Use the TreeView control from the database to dynamically load data

xiaoxiao2021-03-05  24

// ************************************************* *** //

<% @ Page language = "c #" codebehind = "Webform1.aspx.cs" autoeventwireup = "false" inherits = "tretest.webform1"%> <% @ register tagprefix = "@ register tagprefix =" IEWC "Namespace =" Microsoft.Web.ui. WebControls "Assembly =" Microsoft.Web.ui.WebControls "%> < ASP: Label ID = "Label1" Style = "Z-Index: 102; Left: 440px; Position: Absolute; Top: 56px" Runat = "Server" width = "152px" Height = "64px"> label label < / asp: label> Label

// ********************************************************** ******* //

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls Using system.web.ui.htmlcontrols; using system.data.oledb; using microsoft.web.ui.webControls; Namespace Treetest {///

/// WebForm1 summary description. /// public class WebForm1: System.Web.UI.Page {protected Microsoft.Web.UI.WebControls.TreeView TreeView1; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI. WebControls.Label Label2; protected System.Web.UI.WebControls.Label Label3; OleDbConnection Con; string dataConStr = ""; private void Page_Load (object sender, System.EventArgs e) {dataConStr = "Provider = microsoft.jet.oledb. 4.0; data source = " server.mappath (" tree.mdb ") "; user ID =; password =; "; con = new oledbconnection (dattainstr); if (! Ispostback) {Treenode node = new Treenode () Node.text = "root"; node.nodedata = "0"; treeView1.nodes.add (node); // Add root addTestNodes (node); // Add "Expand ..." node}}

private void AddTestNodes (TreeNode node) {TreeNode testnode = new TreeNode (); testnode.Text = "unfolding ...."; testnode.NodeData = "testnode"; node.Nodes.Add (testnode);} private void AddNodes (TREENODE NODE) ​​{String ParentId = "0"; if (node.parent! = Null) {ParentId = node.nodedata;} // DeepTree is a table name string cmdstr = "SELECT * from DeepTree WHERE PARENTID =" ParentID;

OleDbCommand cmd = new OleDbCommand (cmdstr, Con); try {Con.Open (); OleDbDataReader reader = cmd.ExecuteReader (); while (reader.Read ()) {TreeNode childNode = new TreeNode (); childNode.Text = reader ["content"]. TOSTRING (); childnode.nodedata = reader ["id"]. TOSTRING ();

// ***************** Judging whether there is a child node *********** / / / / DeepTree as a table name

cmdstr = "select count (*) from deeptree where parentid =" childNode.NodeData; OleDbConnection Con1 = new OleDbConnection (dataConStr); OleDbCommand cmd1 = new OleDbCommand (cmdstr, Con1); Con1.Open (); OleDbDataReader reader1 = cmd1. ExecuteReader (); if (Reader1.Read ()) {IF (System.Convert.Toint32 (Reader1 [0])> 0) {addTestNodes (childNode); // If there is a child node, add test node}}}} Reader1. CLOSE (); con1.close ();

// ******************************************************** * //

Node.nodes.add (childNode);}} catch (exception ex) {label3.text = ex.Message;} finally {if (con.state! = connectionState.closed) Con. closed ();

}

#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} ///

/// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// private void InitializeComponent () {this.TreeView1.Expand = new Microsoft.Web.UI.WebControls.ClickEventHandler (this.TreeView1_Expand); this.Load = new System.EventHandler (this.Page_Load) ;

} #Endregion

private void TreeView1_Expand (object sender, Microsoft.Web.UI.WebControls.TreeViewClickEventArgs e) {Label1.Text = e.Node; TreeNode node = new TreeNode (); node = TreeView1.GetNodeFromIndex (e.Node); // node = TreeView1.GetNodeFromindex ("0.2"); if (node! = Null) {label2.text = node.nodedata; // Determine the number of subpost sessions under the node, if 1 and for testnode explains unfilled So child node IF ((Node .Nodes.count == 1) && (node.nodes [0] .nodedata == "testnode")) {node.nodes.clear (); // Empty AddNodes (Node); // Fill child node}}} }

Set fields in the database ID, Content, ParentID

PS: Disadvantages, because autopostback is true, each time it is submitted to the server, and the more slower loaded data .. No ASP is fast.

转载请注明原文地址:https://www.9cbs.com/read-34815.html

New Post(0)