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

高兴,学习中量变到质变,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="http://p.blog.csdn.net/images/p_blog_csdn_net/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的为根结点
三、执行后结果

执行后结果

*/

你可能感兴趣的:(UI,.net,F#,asp.net,asp)