[英]ASP.NET TreeView: Unable to get TreeNode.Parent and TreeNode.ChildNodes when using TreeView.TreeNodePopulate
在用户第一次扩展父节点时,我正在填充TreeView
控件的节点,如ASP.NET:如何创建可扩展的空 TreeNode中所述使用按需填充。 填充过程运行良好。
问题是当我尝试获取TreeNode.Parent
时,即使TreeNode
不是根节点,我也会得到null 。 此外,即使指定的TreeNode
有一些子节点, TreeNode.ChildNodes
也会返回一个空集合...
对此有解释吗? 而且,我能做些什么来解决它?
谢谢你。
如果您的用户选择了根父节点,则下面的代码示例应该可以使用数据集合填充树:
受保护的无效 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
集合中的特定值,我添加了额外的代码行来检查 treeview 中的当前节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.