Create a table and insert simulated data:
Create Table [sysmenutree] ([noteid] [decimal] (18, 0) not null, [parentid] [decimal] (18, 0) null, [stext] [nvarchar] (50) null, [svalue] [nvarchar] (50) null, [Surl] [nvarchar] (50) null, [starget] [nvarchar] (50) null, [chger] [nvarchar] (50) null, [chgtime] [nvarchar] (50) null) insert into sysmenutree values (3,0, N 'category done', n' category done', '','') insert into sysmenutree values (, n' category done ', N 'category done', '','') insert into sysmenutree values (, N 'category done', N 'category done ','', '','', '') insert into sysmenutree values (6, 3, n' values. 1', n' category. 1 ', '') insert into sysmenutree values (, n' values. 2 ', N. 2 ', '') insert into sysmenutree values (8, 4, n' values. 1', n' category. 1 ', '') insert into sysmenutree values (9, 4, n' values. 2 ', N. 2 ', '') insert into sysmenutree values (10, 4, n' values. 3', n' category. 3 ', '') insert into sysmenutree values (11,5, n' values. 1', n' category. 1 ', '') insert into sysmenutree values (, n' values. 2 ', N. 2 ','','','','')
Binding data to Treeview in ASP. NET Code :
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
Using system; using system. web. UI. webcontrols; using system. data; using system. data. sqlclient; public partial class _ default: system. web. UI. page {private readonly string connstring = @ "Server =. \ mssqlserver2008; database = chart; uid = sa; Pwd = 123456 "; private datatable dt = NULL; protected void page_load (Object sender, eventargs e) {If (! Ispostback) {dt = new datatable (); getmenutodatatable ("select * From sysmenutree", DT); bindtree (DT, null, "0 ");}} private void bindtree (datatable dtsource, treenode parentnode, string parentid) {datarow [] rows = dtsource. select (string. format ("parentid = {0}", parentid); foreach (datarow row in rows) {treenode node = new treenode (); node. TEXT = row ["stext"]. tostring (); node. value = row ["svalue"]. tostring (); bindtree (dtsource, node, row ["noteid"]. tostring (); If (parentnode = NULL) {treemenu. nodes. add (node);} else {parentnode. childnodes. add (node) ;}} private datatable getmenutodatatable (string query, datatable DT) {using (sqlconnection conn = new sqlconnection (connstring) {sqlcommand cmd = new sqlcommand (query, conn); sqldataadapter Ada = new sqldataadapter (CMD); Ada. fill (DT) ;}return DT ;}}