C # Recursion So the department shows to the TreeView

Source: Internet
Author: User

C # Recursion So the department shows to the TreeView

1. The first is the design of the database table

Create a new Department table: TestUser table

1.ID self-increment int primary key 2.deptname:nchar (3.deptcode:nchar) 4:parentid:nchar (10)

2. The Department table is built.

3. Create a new Web site

4. Drag a TreeView control in the Default.aspx interface of the Web site.

The 5.aspx.cs code is as follows:

public static string strconn=configurationmanager.connectionstrings["ConnString"]. Connectionstring.tostring ();//database connection string protected void Page_Load (Object Sender,eventargs e) {if (!ispostback) {binddepttr EE ("0");//Database you can also design int type}}private void Binddeptnode (TreeNode dtnode) {try{dataset=reds ("Select Deptname,deptcode From TestUser where parentid= ' "+dtnode.value+"), for (int i=0;i<ds. Tables[0].   rows.count;i++) {TreeNode node=new TreeNode (); Node. Text=ds. Tables[0]. rows[i]["Deptname"].   ToString (); Node. Value=ds. Tables[0]. Rows[i][deptcode].   ToString (); DTNODE.CHILDNODES.ADD (node);//Add the specified node to the control Binddeptnode (node);}} catch (Exception ex) {Log.logwrite (ex. Message);}} private void Binddepttree (String parentid) {dataset=reds ("Select Deptname,deptcode from TestUser where parentid= '" + Dtnode.value+ "'"); for (int i=0;i<ds. Tables[0].   rows.count;i++) {TreeNode node=new TreeNode (); Node. Text=ds. Tables[0]. rows[i]["Deptname"].   ToString (); Node. Value=ds. Tables[0]. Rows[i][deptcode]. Tostring(); TVDATA.NODES.ADD (node);//treeview Idtvdata Binddeptnode (node);}} Public DataSet Reds (string strSQL) {using (SqlConnection conn=new SqlConnection (strconn)) {Conn.   Open ();   DataSet ds=new DataSet ();   SqlDataAdapter Da=new SqlDataAdapter (Strsql,conn); Da.   Fill (DS);   Conn.close (); return DS;}}

Running results such as:

Hope to be of help to everyone. There is a shortage of places to look advice. Respect the original, reproduced please indicate the source.

C # Recursion So the department shows to the TreeView

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.