请问TreeView控件中怎么样实现递归?
如果有个xml文件如下:<一级标题>
<二级标题>
<易做图标题>
<四级标题></四级标题>
</易做图标题>
</二级标题>
</一级标题>
节点少时可以foreach中套foreach
但是多了后就麻烦了
所以请教各位教我用一下“递归”怎么实现循环n级标题的
麻烦了!!! --------------------编程问答-------------------- 写一个方法,,自己调用自己就可以了 --------------------编程问答--------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 myLib;
namespace CN.Web
{
public partial class LeftTreeView : System.Web.UI.Page
{
protected DataTable dataTbl1, dataTbl2;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string wherstr="";
if (Request.Cookies["Username"].Value.ToString().ToLower () == "admin" && Request.Cookies["Password"].Value.ToString() == "1")
{
wherstr = "";
}
else
{
if (Session["RoleId"] != null)
{
string roleid = Session["RoleId"].ToString();
wherstr = " where page_id in(select page_id from RolePageRelationship where Role_id=" + roleid + ")";
}
}
InitRootNodeDataTable(wherstr);
initTree(dataTbl1);
//tvCategory.CollapseAll();
}
}
/// <summary>
/// 初始化 RootNode DataTable
/// </summary>
private void InitRootNodeDataTable(string wherstr)
{
dataTbl1 = new DataTable();
dataTbl1 = DbAccess.GetDS("select * from page " + wherstr).Tables[0];
//读取并将公共的分类信息添加到数据集中
dataTbl1.TableName = "TreeView";
}
/// <summary>
/// 初始化树
/// </summary>
/// <param name="dt">取得所有的分类</param>
private void initTree(DataTable dt)
{
this.tvCategory.Nodes.Clear();//先清理原先的
if (dt != null && dt.Rows.Count == 0) return;
DataRow[] Rows = dt.Select("Page_parent=0 ");
if (Rows.Length == 0) return;
foreach (DataRow row in Rows)
{
TreeNode tNode = new TreeNode();
tNode.Value = row["Page_id"].ToString();
tNode.Text = row["Page_name"].ToString();
//tNode.NavigateUrl = "";
//tNode.Target = "MainFrameBS";//指向父窗体右边的那个iframe
this.tvCategory.Nodes.Add(tNode);
tNode.Expanded = true;
createChildNode(tNode, tNode.Value);
}
////添加一级“未分类”节点
//TreeNode tNode1 = new TreeNode();
//tNode1.Value = "-1";
//tNode1.Text = "未分类";
//tNode1.NavigateUrl = "BasicInfoConfig.aspx?Class_ID=" + tNode1.Value + "";
//tNode1.Target = "MainFrameBS";//指向父窗体右边的那个iframe
//this.tvCategory.Nodes.Add(tNode1);
//tNode1.Expanded = true;
}
/// <summary>
/// 创建子节点
/// </summary>
/// <param name="node"></param>
/// <param name="strParentNo"></param>
private void createChildNode(TreeNode node, string strParentNo)
{
//string parentId="";
DataRow[] Rows = this.dataTbl1.Select("Page_parent='" + strParentNo + "' and Page_parent<>0");
foreach (DataRow row in Rows)
{
TreeNode childNode = new TreeNode();
childNode.Value = row["Page_id"].ToString();
childNode.Target = "mainFrame";//指向父窗体右边的那个iframe
childNode.Text = row["Page_name"].ToString();
childNode.NavigateUrl = "~/"+row["Page_url"].ToString(); ;
node.ChildNodes.Add(childNode);
createChildNode(childNode, row["Page_id"].ToString());
}
}
}
}
补充:.NET技术 , C#