高兴,学习中量变到质变,ASP.NET 2.0 树型数据的显示源代码(原创,用递归)

本文介绍如何使用ASP.NET 2.0实现树型数据的递归加载,通过示例代码展示了从数据库获取树型结构数据并将其展示在TreeView控件上的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

高兴,学习中量变到质变,ASP.NET 2.0 树型数据的显示源代码(原创,用递归)

using System;
using System.Data;
using System.Data.SqlClient;
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;



public partial class _Default:System.Web.UI.Page
... {

SqlConnectionconn
=newSqlConnection("workstationid="cyz";userid=sa;password=****;initialcatalog=pubs;persistsecurityinfo=false");
DataSetds
=newDataSet();

protectedvoidPage_Load(objectsender,EventArgse)
...{
if(!(IsPostBack))
...{
SqlDataAdapterda
=newSqlDataAdapter("select*fromt_treeorderbyparentid",conn);
da.Fill(ds,
"t_tree");
InitTree0();


}

}

privatevoidInitTree0()//根结点加载函数
...{
TV.Nodes.Clear();
//TV为TreeView控件
DataRow[]rows=ds.Tables["t_tree"].Select("parentid=0");
for(inti=0;i<rows.Length;i++)
...{
TreeNodeT_root
=newTreeNode();
DataRowdr
=rows[i];
T_root.Text
=dr["Descricpt"].ToString();
TV.Nodes.Add(T_root);
InitTree(T_root,dr[
"id"].ToString());//根结点加载完后,开始调用加载子结点的函数,并开始递归。
}

}

privatevoidInitTree(TreeNodeNd,StringParent_id)//子树结点加载函数
...{
DataRow[]rows
=ds.Tables["t_tree"].Select("parentid="+Parent_id);
if(rows!=null)
...{
for(inti=0;i<rows.Length;i++)
...{
TreeNodeTnd
=newTreeNode();
DataRowdr
=rows[i];
Tnd.Text
=dr["Descricpt"].ToString();
Nd.ChildNodes.Add(Tnd);
InitTree(Tnd,dr[
"id"].ToString());//递归调用
}

}

}

}

/*后注:
一、T_Tree表生成脚本
CREATE TABLE [dbo].[T_Tree] (
[ID] [int] NOT NULL ,
[ParentID] [int] NOT NULL ,
[Descricpt] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
二、T_Tree表数据
<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><img alt="T_Tree表数据" src="https://p-blog.youkuaiyun.com/images/p_blog_youkuaiyun.com/cyz1980/T_Tree%E8%A1%A8%E6%95%B0%E6%8D%AE.JPG"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 117.75pt; HEIGHT: 110.25pt" type="#_x0000_t75"><imagedata o:title="T_Tree表数据" src="file:///E:/DOCUME~1/cap/LOCALS~1/Temp/msoclip1/01/clip_image001.jpg"></imagedata></shape>
Parentid=0的为根结点
三、执行后结果

执行后结果

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值