Accumulated [ASP. NET]-ASP. NET Treeview read database

Source: Internet
Author: User
1   Front-end:  2 <% @ Page Language = "  C #  " Autoeventwireup = "  True  " Codebehind = "  Default. aspx. CS  " Inherits = "  Treeview. _ default  " %>3   4 <! Doctype HTML public "  -// W3C // dtd xhtml 1.0 transitional // en  "   "  Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd  " > 5   6 <HTML xmlns = "  Http://www.w3.org/1999/xhtml  " > 7 <Head runat ="  Server  " > 8 <Title> </title> 9 </Head> 10 <Body> 11 <Form ID = "  Form1  " Runat = "  Server  " > 12 <Div>13 <Asp: Treeview id = "  Treeview1  " Runat = "  Server  " Showlines = "  True  " > 14 </ASP: Treeview> 15 </Div> 16 </Form> 17 </Body> 18 </Html> 19   20   21   22   Background:  23   Using  System;  24   Using  System. Collections. Generic;  25   Using  System. LINQ;  26  Using  System. Web;  27   Using  System. Web. UI;  28   Using  System. Web. UI. webcontrols;  29   Using  System. Data;  30   Using  System. Data. sqlclient;  31   Using System. configuration;  32   33   Namespace  Treeview  34   {  35       Public   Partial   Class  _ Default: system. Web. UI. Page  36   {  37           Public   Static  String St = configurationmanager. connectionstrings [ "  SQL  "  ]. Tostring ();  38           Private Datatable DTS = New  Datatable ();  39           Protected   Void Page_load ( Object  Sender, eventargs E)  40  {  41               If (! Ispostback)  42   {  43 DTS = Createtable ();  44   Createnode ();  45   }  46   }  47           Public  Void  Createnode ()  48   {  49 Datarow [] DR = DTS. Select ( "  Parentid = 0  "  );  50               If (Dr. length> 0  )  51   {  52                  Foreach (Datarow DRR In  Dr)  53   {  54 Treenode Tn = New  Treenode ();  55 Tn. value = DRR [ "  Menuid  "  ]. Tostring ();  56 Tn. Text = DRR ["  Menuname  "  ]. Tostring ();  57 Tn. Expanded = False  ;  58 Tn. selectaction = Treenodeselectaction. Expand;  59   Treeview1.nodes. Add (TN );  60   Createchild (TN, DTS );  61  }  62   }  63               Else  64   {  65 Treenode T = New  Treenode ();  66 T. value = "  Null  "  ;  67 T. Text = "  Null  "  ;  68 T. Expanded = False  ;  69 T. selectaction = Treenodeselectaction. Expand;  70   Treeview1.nodes. Add (t );  71   }  72  }  73           Public   Void  Createchild (treenode tnn, datatable DTT)  74   {  75 Datarow [] DR = DTT. Select ( "  Parentid =  " + Tnn. value );  76               If (Dr. length> 0 )  77   {  78                   Foreach (Datarow drw In  Dr)  79   {  80 Treenode Ts = New  Treenode ();  81 TS. value = drw [ "  Menuid "  ]. Tostring ();  82 TS. Text = drw [ "  Menuname  "  ]. Tostring ();  83 TS. selectaction = Treenodeselectaction. Expand;  84 TS. Expanded = False  ;  85   Tnn. childnodes. Add (TS ); 86   Createchild (TS, DTT );  87   }  88   }  89   }  90           Public  Datatable createtable ()  91   {  92 Datatable d = New Datatable ();  93               Using (Sqlconnection SQL = New  Sqlconnection (ST ))  94   {  95                   96 Sqlcommand sq = New Sqlcommand ( "  Select * From treeviewname  "  , SQL );  97 Sqldataadapter SDA = New  Sqldataadapter ();  98 SDA. selectcommand = Sq;  99   SDA. Fill (d );  100                   101   }  102               Return  D;  103   }  104  }  105 }

 

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.