ASP.NET TreeView:使用 TreeView.TreeNodePopulate 时无法获取 TreeNode.Parent 和 TreeNode.ChildNodes

发布于 2024-11-24 06:54:18 字数 445 浏览 2 评论 0原文

当用户第一次展开父节点时,我正在填充 TreeView 控件的节点,使用按需填充,如 ASP.NET:如何创建可扩展空树节点。填充过程运行良好。

问题是,当我尝试获取 TreeNode.Parent 时,即使 TreeNode 不是根节点,我也会得到 null 。另外,即使指定的 TreeNode 有一些子节点,TreeNode.ChildNodes 也会返回一个空集合...

对此有解释吗?并且,我能做些什么来解决它?

谢谢。

I am populating a TreeView control's nodes at the moment user expands the parent node - the first time - using populate on demand as described in ASP.NET: How to Create an Expandable Empty TreeNode. The population process works fine.

The problem is when I try to get the TreeNode.Parent I get a null even if the TreeNode was not a root node. Also, TreeNode.ChildNodes returns an empty collection even if there were some child nodes for the specified TreeNode ...

Is there an explanation for this? And, what can I do to solve it ?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞎闹 2024-12-01 06:54:18

如果您的用户已选择根父节点,则下面的代码示例应该可以使用数据集合填充树:

protected void ParseTreeNodes(object sender, 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 集合中的特定值,请编写代码来检查树视图中的当前节点。

If your user has selected the root parent node, this code example below should work to populate the tree using a collection of data:

protected void ParseTreeNodes(object sender, 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);
    }
}

I added an additional line of code to inspect the current nodes in the treeview if you are having an issue with the parent node or knowing the specific values in the Nodes collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文