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

时间:2022-06-22 18:11:57

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

第一种:性能不好

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

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

第二步:前台代码

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="tree.aspx.cs" Inherits="Maticsoft.Web.tree" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function show(msg) {
alert(msg);
}
</script> </head>
<body> <form id="form1" runat="server">
<div>
<asp:TreeView ID="treeT" runat="server">
</asp:TreeView> </div>
</form>
</body>
</html>

第三步:后台代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient; namespace Maticsoft.Web
{
public partial class tree : System.Web.UI.Page
{
Maticsoft.BLL.G_USERS bll = new BLL.G_USERS();
Maticsoft.Model.G_USERS model = new Model.G_USERS(); protected void Page_Load(object sender, EventArgs e)
{
//根节点的条件
BindTree("");
} #region 绑定父节点(第一级)
private void BindTree(string pid)
{ DataSet ds = bll.GetList("a.status > -1 and utype=" + pid);
if (ds.Tables[].Rows.Count > )
{
for (int i = ; i < ds.Tables[].Rows.Count; i++)
{
TreeNode node = new TreeNode();
node.Text = ds.Tables[].Rows[i]["uName"].ToString();
node.Target = ds.Tables[].Rows[i]["dID"].ToString();
this.treeT.Nodes.Add(node);
BindNode(node);
}
}
}
#endregion #region 绑定子节点 #endregion
private void BindNode(TreeNode nd)
{
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 ");
for (int i = ; i < ds.Tables[].Rows.Count; i++)
{
TreeNode node = new TreeNode();
node.Text = ds.Tables[].Rows[i]["uName"].ToString();
node.Target = ds.Tables[].Rows[i]["dID"].ToString();
nd.ChildNodes.Add(node); //判断是否到最底层节点
if (ds.Tables[].Rows[i]["utype"].ToString() != "")
{
BindNode(node);
}
}
} }
}

效果功能图:

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

另一种一步到位:(一次把所有数据放在数据集中,后面再查询)

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace tree
{
public partial class tree : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataSet ds = GetData();
DataRow[] dr = ds.Tables[].Select("utype=8");
TreeNode node = new TreeNode();
node.Text = dr[]["UName"].ToString();
node.Value = dr[]["ID"].ToString();
this.TreeView1.Nodes.Add(node);
BindTree(node, dr[]["DID"].ToString(), ds);
}
} private void BindTree(TreeNode Nodes, string pid, DataSet ds)
{
DataRow[] dr = ds.Tables[].Select("fid=" + pid, "shorder asc");
if (dr.Length > )
{
for (int i = ; i < dr.Length; i++)
{
TreeNode node = new TreeNode();
node.Text = dr[i]["UName"].ToString();
node.Value = dr[i]["ID"].ToString();
Nodes.ChildNodes.Add(node);
if(dr[i]["utype"].ToString() !="")
BindTree(node, dr[i]["DID"].ToString(), ds);
}
}
} private DataSet GetData()
{
OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.AppSettings["OleDbConnString"]);
conn.Open();
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 "; OleDbDataAdapter oda = new OleDbDataAdapter(osqlstr, conn); DataSet ods = new DataSet();
oda.Fill(ods); return ods;
}
}
}