六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 75|回复: 0

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

[复制链接]

升级  3.95%

345

主题

345

主题

345

主题

探花

Rank: 6Rank: 6

积分
1079
 楼主| 发表于 2013-2-5 09:05:39 | 显示全部楼层 |阅读模式
高兴,学习中量变到质变,ASP.NET 2.0 树型数据的显示源代码(原创,用递归)
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;



publicpartialclass_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;
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;
Tnd.Text
=dr["Descricpt"].ToString();
Nd.ChildNodes.Add(Tnd);
InitTree(Tnd,dr[
"id"].ToString());//递归调用
}
}

}

}

您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表