Happy, learning changes from quantitative to qualitative, ASP. NET 2.0 tree data displaySource code(Original, recursive)
Using System;
Using System. Data;
Using System. Data. sqlclient;
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
... {
Sqlconnection Conn = New Sqlconnection ( " Workstation id = "cyz"; user id = sa; Password = ***; initial catalog = pubs; persist Security info = false " );
Dataset DS = New Dataset ();
Protected Void Page_load ( Object Sender, eventargs E)
... {
If ( ! (Ispostback ))
... {
Sqldataadapter da = New Sqldataadapter ( " Select * From t_tree order by parentid " , Conn );
Da. Fill (DS, " T_tree " );
Inittree0 ();
}
}
Private Void Inittree0 () // Root Node Loading Function
... {
TV. nodes. Clear (); // TV is the Treeview Control
Datarow [] rows = DS. Tables [ " T_tree " ]. Select ( " Parentid = 0 " );
For ( Int I = 0 ; I < Rows. length; I ++ )
... {
Treenode t_root = New Treenode ();
Datarow Dr = Rows [I];
T_root.text = Dr [ " Descricpt " ]. Tostring ();
TV. nodes. Add (t_root );
Inittree (t_root, Dr [ " ID " ]. Tostring ()); // After the root node is loaded, call the function to load the child node and start recursion.
}
}
Private Void Inittree (treenode nd, string parent_id) // Subtree node loading Function
... {
Datarow [] rows = DS. Tables [ " T_tree " ]. Select ( " Parentid = " + Parent_id );
If (Rows ! = Null )
... {
For ( Int I = 0 ; I < Rows. length; I ++ )
... {
Treenode TND = New Treenode ();
Datarow Dr = Rows [I];
TND. Text = Dr [ " Descricpt " ]. Tostring ();
Nd. childnodes. Add (TND );
Inittree (TND, Dr [ " ID " ]. Tostring ()); // Recursive call
}
}
}
}
/* Post note:
I. t_tree Table Generation script
Create Table [DBO]. [t_tree] (
[ID] [int] not null,
[Parentid] [int] not null,
[Descricpt] [varchar] (50) Collate chinese_prc_ci_as null
) On [primary]
Go
Ii. t_tree table data
Parentid = 0 is the root node
Iii. Post-execution results
*/