zoukankan      html  css  js  c++  java
  • TreeView树形控件递归绑定数据库里的数据

    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 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 }
  • 相关阅读:
    application.properties多环境配置文件、jar包外部配置文件、配置项加密、程序中配置使用
    SpringBoot项目打war包部署Tomcat教程
    spring boot 集成 redis lettuce
    spring boot maven打包可运行jar包
    IDEA项目搭建十四——Web站点Controller基类及布局页静态资源设计
    Nginx Windows详细安装部署教程
    多线程编程CompletableFuture与parallelStream
    IDEA项目搭建十三——服务消费端与生产端通信实现
    【异常】MySQL建表报错:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"order"' at line 1
    【警告】mysql链接警告信息:Establishing SSL connection without server's identity verification is not recommended
  • 原文地址:https://www.cnblogs.com/fjptwwf/p/5404132.html
Copyright © 2011-2022 走看看