TreeView樹形控件遞歸綁定數據庫裏的數據。javascript
第一種:性能很差html
第一步:數據庫中查出來的表,字段名分別爲UNAME(顯示名稱),DID(關聯數據),UTYPE(類型)java
第二步:前臺代碼node
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>
第三步:後臺代碼sql
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 using System.Data.OleDb; 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 if (!Page.IsPostBack) 17 { 18 DataSet ds = GetData(); 19 DataRow[] dr = ds.Tables[0].Select("utype=8"); 20 TreeNode node = new TreeNode(); 21 node.Text = dr[0]["UName"].ToString(); 22 node.Value = dr[0]["ID"].ToString(); 23 this.TreeView1.Nodes.Add(node); 24 BindTree(node, dr[0]["DID"].ToString(), ds); 25 } 26 } 27 28 private void BindTree(TreeNode Nodes, string pid, DataSet ds) 29 { 30 DataRow[] dr = ds.Tables[0].Select("fid=" + pid, "shorder asc"); 31 if (dr.Length > 0) 32 { 33 for (int i = 0; i < dr.Length; i++) 34 { 35 TreeNode node = new TreeNode(); 36 node.Text = dr[i]["UName"].ToString(); 37 node.Value = dr[i]["ID"].ToString(); 38 Nodes.ChildNodes.Add(node); 39 if(dr[i]["utype"].ToString() !="0") 40 BindTree(node, dr[i]["DID"].ToString(), ds); 41 } 42 } 43 } 44 45 private DataSet GetData() 46 { 47 OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.AppSettings["OleDbConnString"]); 48 conn.Open(); 49 string osqlstr ="select a.id,a.uname,a.utype,b.id did,b.fid,b.shorder from g_users A,G_DEPT B where a.status>-1 and a.id=b.user_id "; 50 51 OleDbDataAdapter oda = new OleDbDataAdapter(osqlstr, conn); 52 53 DataSet ods = new DataSet(); 54 oda.Fill(ods); 55 56 return ods; 57 } 58 } 59 }