Happy, learning to change from quantitative to qualitative, ASP. NET 2.0 tree data display source code (original, recursive)

Source: Internet
Author: User
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



*/

 

Related Article

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.