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