ASP. NET read database binding to Treeview recursion Mode

Source: Internet
Author: User

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 ;}}

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.