您的位置:首页 > 编程语言 > ASP

asp.net 动态添加 treeview 递归

2013-11-11 14:22 393 查看
http://blog.csdn.net/wangyonghua_net/article/details/7693088

eg1:

/// <summary>

/// 绑定大树

/// </summary>

public void BindTvClass()

{

this.tvClass.Nodes.Clear();

TreeNode node = new TreeNode();

node.Text = "栏目";

node.Value = "0";

AddChild(node);

this.tvClass.Nodes.Add(node);

}

/// <summary>

/// tvClass绑定数据

/// </summary>

/// <param name="parentNode"></param>

public void AddChild(TreeNode parentNode)

{

IList<PubClassInfo> classes = transcate.GetClassChild(parentNode.Value);

foreach (PubClassInfo item in classes)

{

TreeNode childNode = new TreeNode();

childNode.Text = item.ClassCName;

childNode.Value = item.ClassID;

//判断是否有子节点

IList<PubClassInfo> classesChild = transcate.GetClassChild(childNode.Value);

if (classesChild.Count != 0)

{

AddChild(childNode);

}

parentNode.ChildNodes.Add(childNode);

}

}

如果在每个节点前加checkbox

aspx:ShowCheckBoxes="Leaf"

cs;TreeNodeCollection nodeList = tvClass.CheckedNodes;//得到所有选中复选框的节点集合

for (int i = 0; i < nodeList.Count; i++)

{

strClasses[i] = nodeList[i].Value;

}

eg2:

public void AddChild(TreeNode nodeParent)

{

NHibernateHelper nhHelper = new NHibernateHelper();

ISessionFactory factory = nhHelper.GetSessionFactory();

ISession session = factory.OpenSession();

ItemList itls = new ItemList(session);

ITransaction tx = session.BeginTransaction();

try

{

IList<NHibernate.Domain.Organization> orgs = itls.GetOrganizationChild(nodeParent.Value);

foreach (var item in orgs)

{

TreeNode childNode = new TreeNode();

childNode.Text = item.OrganizationName;

childNode.Value = item.OrganizationID.ToString();

nodeParent.ChildNodes.Add(childNode);

//判断是否有子节点

IList<NHibernate.Domain.Organization> orgsChild = itls.GetOrganizationChild(item.OrganizationID.ToString());

if (orgsChild.Count!=0)

{

AddChild(childNode);

}

}

tx.Commit();


}

catch (Exception)

{

tx.Rollback();

throw;

}

finally

{

session.Close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: