惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - GO-NET

jQuery选择器总结(转) Js操作Excel常用方法 DataView不能按中文排序问题解决 浮动层居中的对话框效果演示 javascript获得图片的大小 长宽 查找父元素和子元素 ajax form提交 javascript设置分页 url参数为page 网页鼠标提示 写一个触发器。从tb_control里删除一条记录,自动把该记录添加到另一个表tb_ctrlHistor中 sql读取指定字符前的字符 数据库对象命名参考 【转】 正则表达式入门教程 权限设计 【数据库和代码】 C#编码建议 【程序权限设计】 ASP.NET设置ie打印两法 生意就是这样做成的 SQL Server 存储过程的经典分页
无限级treeview设计
GO-NET · 2007-08-29 · via 博客园 - GO-NET

无限级treeview设计

数据库设计

aspx页面如下

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:TreeView ID="TreeView1" runat="server" ImageSet="BulletedList3" ShowLines="True">
        
</asp:TreeView>
    
    
</div>
    
</form>
</body>
</html>

代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!this.IsPostBack)
        {
            
this.buildTree();
        }
    }
/// <summary>
    
/// 创建树
    
/// </summary>
    private void buildTree()
    {

        DataSet ds 

= ExecuteDataSet("select * from treetest");//建立关系
        ds.Relations.Add("SortRelation", ds.Tables[0].Columns["treeid"], ds.Tables[0].Columns["treeparentid"]);//遍历每行并根据数据行关系生成树
        foreach (DataRow dbRow in ds.Tables[0].Rows)
        {
            
if (dbRow.IsNull("treeparentid"))
            {
                
string s = dbRow["treename"].ToString();
                TreeNode tn 
= new TreeNode(s);
                tn.NavigateUrl 
= "#";
                
this.TreeView1.Nodes.Add(tn);
                PopulateSubTree(dbRow, tn);

            }
        }

         
    }

/// <summary>
    
/// 遍历子类
    
/// </summary>
    
/// <param name="dbRow">遍历的Row</param>
    private void PopulateSubTree(DataRow dbRow, TreeNode tn)
    {
       
        
foreach (DataRow childRow in dbRow.GetChildRows("SortRelation"))
        {
            
string s = "";

            s 

= childRow["treename"].ToString();
            TreeNode tn2
=new TreeNode(s);
            tn.ChildNodes.Add(tn2);
       
            PopulateSubTree(childRow, tn2);
        }
    }
#region  返回dataset对象
    
/// <summary>
    
/// 返回dataset对象
    
/// </summary>
    
/// <param name="sql">sql语句</param>
    
/// <returns></returns>
    public DataSet ExecuteDataSet(string sql)
    {
        SqlConnection con 
= new SqlConnection("server=.;database=treeviewtest;uid=sa;pwd=;");
        DataSet ds 
= new DataSet();
        SqlDataAdapter sda 
= new SqlDataAdapter(sql, con);
        sda.Fill(ds);
        
return ds;
    }
    
#endregion
}

显示结果