简体   繁体   English

在ASP.NET TreeView控件的所有树节点上设置SelectAction

[英]Set SelectAction on all tree nodes for ASP.NET TreeView control

I have a TreeView web control on an ASP page. 我在ASP页面上有一个TreeView Web控件。 I am programmatically populating all the tree nodes. 我以编程方式填充所有树节点。 I want to disable the link on ALL tree nodes. 我想禁用所有树节点上的链接。 I can do it one at a time, like this (using a string array for simplicity's sake): 我可以一次做一个,就像这样(为简单起见,使用字符串数组):

for each (string strValue in strValues)
{
TreeNode objNode = new TreeNode(strValue);
objNode.SelectAction = TreeNodeSelectAction.None;
objTreeView.Nodes.Add(objNode);
}

Assume for the sake of argument that I have multiple level of nodes, so there is not a simple way to iterate through all nodes once I am done populating. 假设为了论证我有多个级别的节点,所以一旦我完成填充,就没有一种简单的方法来遍历所有节点。 Is there a property I can set on the TreeView to set SelectAction for all nodes? 我可以在TreeView上设置一个属性来为所有节点设置SelectAction吗?

TreeView does not support any property to do this. TreeView不支持任何属性来执行此操作。 However you can do it by using recursive methods 但是,您可以使用递归方法来完成此操作

This should solve your problem: 这应该可以解决您的问题:

    protected void Page_Load(object sender, EventArgs e)
    {
        processNode(trvTest.Nodes);
    }

    private void processNode(TreeNodeCollection nodes)
    {
        foreach (TreeNode node in nodes)
        {
            node.SelectAction = TreeNodeSelectAction.None;
            if (node.ChildNodes.Count > 0)
                processNode(node.ChildNodes);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM