码迷,mamicode.com
首页 > 数据库 > 详细

TreeView树形控件递归绑定数据库里的数据

时间:2016-04-18 15:04:36      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:

TreeView树形控件递归绑定数据库里的数据。

第一步:数据库中查出来的表,字段名分别为UNAME(显示名称),DID(关联数据),UTYPE(类型)

技术分享

 

第二步:前台代码

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="tree.aspx.cs" Inherits="Maticsoft.Web.tree" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <title></title>
 8     <script type="text/javascript">
 9         function show(msg) {
10             alert(msg);
11         }
12     </script>
13 
14 </head>
15 <body>
16     
17     <form id="form1" runat="server">
18     <div>
19         <asp:TreeView ID="treeT" runat="server">
20         </asp:TreeView>
21 
22 
23     </div>
24     </form>
25 </body>
26 </html>

 

第三步:后台代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Data;
 8 using System.Data.SqlClient;
 9 
10 namespace Maticsoft.Web
11 {
12     public partial class tree : System.Web.UI.Page
13     {
14         Maticsoft.BLL.G_USERS bll = new BLL.G_USERS();
15         Maticsoft.Model.G_USERS model = new Model.G_USERS();
16 
17         protected void Page_Load(object sender, EventArgs e)
18         {
19            //根节点的条件
20             BindTree("8");  
21         }
22 
23         #region 绑定父节点(第一级)
24         private void BindTree(string pid)
25         {
26             
27             DataSet ds = bll.GetList("a.status > -1 and utype=" + pid);
28             if (ds.Tables[0].Rows.Count > 0)
29             {
30                 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
31                 {
32                     TreeNode node = new TreeNode();
33                     node.Text = ds.Tables[0].Rows[i]["uName"].ToString();
34                     node.Target = ds.Tables[0].Rows[i]["dID"].ToString();
35                     this.treeT.Nodes.Add(node);
36                     BindNode(node);
37                 }
38             }
39         }
40         #endregion 
41 
42         #region 绑定子节点
43         
44         #endregion
45         private void BindNode(TreeNode nd)
46         {
47             DataSet ds = bll.GetList("a.status>-1 and  a.id=b.user_id and b.fid=" + Convert.ToString(nd.Target) + " order by b.shorder asc ");
48             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
49             {
50                 TreeNode node = new TreeNode();
51                 node.Text = ds.Tables[0].Rows[i]["uName"].ToString();
52                 node.Target = ds.Tables[0].Rows[i]["dID"].ToString();
53                 nd.ChildNodes.Add(node);
54 
55                 //判断是否到最底层节点
56                 if (ds.Tables[0].Rows[i]["utype"].ToString() != "0")
57                 {
58                     BindNode(node);
59                 }
60             }
61         }
62         
63 
64 
65      
66     }
67 }

 

效果功能图:

技术分享

 

 

另一种一步到位:(数据太大会超时)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Data;
 8 using System.Data.SqlClient;
 9 
10 namespace tree
11 {
12     public partial class tree : System.Web.UI.Page
13     {
14         protected void Page_Load(object sender, EventArgs e)
15         {
16             BindTree("8");
17         }
18         #region 绑定父节点(第一级)
19         private void BindTree(string pid)
20         {
21             DataSet ds = GetData(pid);
22             if (ds.Tables[0].Rows.Count > 0)
23             {
24                 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
25                 {
26                     TreeNode node = new TreeNode();
27                     node.Text = ds.Tables[0].Rows[i]["UName"].ToString();
28                     node.Target = ds.Tables[0].Rows[i]["DID"].ToString();
29                     this.TreeView1.Nodes.Add(node);
30                     BindNode(node);
31                 }
32             }
33         }
34         #endregion
35 
36         #region 绑定子节点
37         private void BindNode(TreeNode nd)
38         {
39             DataSet ds = GetData(Convert.ToString(nd.Target));
40             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
41             {
42                 TreeNode node = new TreeNode();
43                 node.Text = ds.Tables[0].Rows[i]["UName"].ToString();
44                 node.Target = ds.Tables[0].Rows[i]["DID"].ToString();
45                 nd.ChildNodes.Add(node);
46                 if (ds.Tables[0].Rows[i]["utype"].ToString() != "0")
47                 {
48                     BindNode(node);
49                 }
50             }
51         }
52         #endregion
53 
54         #region 将父级下的子级查询出来,并返回一张虚拟表
55         private DataSet GetData(string pid)
56         {
57             string connstr = "server=192.168.1.74;database=dsoa;uid=dsoa;pwd=dreamsoft;";
58             SqlConnection con = new SqlConnection(connstr);
59             con.Open();
60             string sqlstr =string.Empty;
61             if (pid == "8")
62             {
63                 sqlstr = "select a.uname, b.id did, utype from g_users A left join G_DEPT B on a.id=b.user_id where a.status>-1 and utype =" + pid;
64             }
65             else
66             {
67                 sqlstr = "select a.uname,b.id did, utype from g_users A,G_DEPT B where a.status>-1 and  a.id=b.user_id and b.fid=" + pid + " order by b.shorder asc ";
68             }
69             SqlDataAdapter da = new SqlDataAdapter(sqlstr,con);
70             DataSet ds = new DataSet();
71             da.Fill(ds);
72             return ds;
73         }
74         #endregion   
75 
76     }
77 }

 

TreeView树形控件递归绑定数据库里的数据

标签:

原文地址:http://www.cnblogs.com/fjptwwf/p/5404132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!