TreeView
在用户第一次扩展父节点时,我正在填充控件的节点,如ASP.NET: How to Create an Expandable Empty TreeNode中所述,使用按需填充。填充过程运行良好。
问题是当我尝试获取TreeNode.Parent
null时,即使它TreeNode
不是根节点。此外,即使指定的...TreeNode.ChildNodes
有一些子节点,也会返回一个空集合。TreeNode
对此有解释吗?而且,我能做些什么来解决它?
谢谢你。
TreeView
在用户第一次扩展父节点时,我正在填充控件的节点,如ASP.NET: How to Create an Expandable Empty TreeNode中所述,使用按需填充。填充过程运行良好。
问题是当我尝试获取TreeNode.Parent
null时,即使它TreeNode
不是根节点。此外,即使指定的...TreeNode.ChildNodes
有一些子节点,也会返回一个空集合。TreeNode
对此有解释吗?而且,我能做些什么来解决它?
谢谢你。
如果您的用户选择了根父节点,则下面的代码示例应该可以使用数据集合填充树:
protected void ParseTreeNodes(对象发送者,System.Web.UI.WebControls.TreeNodeEventArgs e)
{
//Uncomment this line below and inspect the 'nodes' variable if you need to inspect all of the nodes
//var nodes = this.MyTreeView.Nodes;
//Some call to get data for example
var dataCollection = GetSomeDataForTheTree()
//Iterate through and create a new node for each row in the query results.
//Notice that the query results are stored in the table of the DataSet.
foreach (MyClassWithData data in dataCollection)
{
//Create the new node using the values from the collection:
TreeNode newNode = new TreeNode(data.LastName, system.ID)
{
//Set the PopulateOnDemand property to false, because these are leaf nodes
and do not need to be populated on subsequent calls.
PopulateOnDemand = false,
SelectAction = TreeNodeSelectAction.Select
};
//Add the new node to the ChildNodes collection of the parent node.
e.node.ChildNodes.Add(NewNode);
}
}
如果您遇到父节点问题或知道Nodes
集合中的特定值,我添加了额外的代码行来检查树视图中的当前节点。