简体   繁体   English

如何在aspx树视图中按值设置所选节点

[英]How to set selected node by value in aspx Tree view

How to set selected node by value in aspx Tree view I tried the following code but this is working first level of the tree 如何在aspx树视图中按值设置所选节点,我尝试了以下代码,但这在树的第一层工作

foreach (TreeNode node in tvLocations.Nodes[0].ChildNodes)
{
    if (node.Value == LocId.ToString())
    {
        node.Select();
    }
}

I also tried this 我也尝试过

tvLocations.SelectedNode.Value = LocId.ToString();
tvLocations.SelectedNode.Value = LocId.ToString();
tvLocations.SelectedNode.Select(); 

but not working. 但不起作用。

I want something like as we set in dropdown 我想要类似我们在下拉菜单中设置的内容

dropdown1.selectedValue="5";

Please help me 请帮我

A nice recursive function does the trick: 一个不错的递归函数可以解决这个问题:

<asp:textbox id="txtFind" runat="server" />
<asp:button id="btnFind" runat="server" text="Go" onclick="btnFind_Click" />
<asp:treeview id="tvHierarchy" runat="server" datasourceid="dsHierarchy" nodestyle-cssclass="treeviewnode" parentnodestyle-cssclass="parentnode" selectednodestyle-cssclass="selectednode" autogeneratedatabindings="false">
    <databindings>
         <asp:treenodebinding datamember="employee" textfield="fullname" populateondemand="true" valuefield="login" selectaction="SelectExpand" />
    </databindings>
</asp:treeview>

protected void btnFind_Click(object sender, EventArgs e) {
    SelectNodeByValue(tvHierarchy.Nodes[0], txtFind.Text); 
}

protected void SelectNodeByValue(TreeNode Node, string ValueToSelect
{
    foreach (TreeNode n in Node.ChildNodes)
    {
        if (n.Value == ValueToSelect)
        {
            n.Select();
        }
        else
        {
            SelectNodeByValue(n, ValueToSelect);
        }
    }
}

You can use : 您可以使用 :

node.Selected = true; node.Selected = true;

Have a look at the below code sample : 看看下面的代码示例:

ASPX Page : ASPX页面:

<asp:TreeView runat="server" ID="tvLocations">
    <Nodes>
        <asp:TreeNode Text="Node 1" Value="Node 1"></asp:TreeNode>
        <asp:TreeNode Text="Node 2" Value="Node 2">
            <asp:TreeNode Text="Child Node 1" Value="Child Node 1"></asp:TreeNode>
            <asp:TreeNode Text="Child Node 2" Value="Child Node 2"></asp:TreeNode>
        </asp:TreeNode>
        <asp:TreeNode Text="Node 3" Value="Node 3"></asp:TreeNode>
        <asp:TreeNode Text="Node 4" Value="Node 4"></asp:TreeNode>
    </Nodes>
    <NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="0px"
        NodeSpacing="0px" VerticalPadding="0px" />
    <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px"
        VerticalPadding="0px" />
</asp:TreeView>

Update : Code Behind : 更新: 后台代码:

var nodeValue = "Child Node 1";
foreach (TreeNode node in tvLocations.Nodes)
{

    if (node.ChildNodes.Count > 0)
    {
        foreach (TreeNode child in node.ChildNodes)
        {
            if (child.Value == nodeValue)
            {
                child.Selected = true;
            }
        }
    }
    else if(node.Value == nodeValue)
    {
        node.Selected = true;
    }
}

I have tried this is working for me for five level in tree view can you tell me any easeir way than this 我已经尝试过在树形视图中这对我来说是五个级别的工作,您能告诉我任何比这更简单的方法

 var locId = Convert.ToInt32(e.CommandArgument);
         foreach (TreeNode node in tvLocations.Nodes)
        {
            //level 1
            bool value = false;
            if (value)
                break;
            if (node.Value == locId.ToString())
            {
                node.Selected = true;
                value = true;
                break;
            }
            else 
            {
                if (node.ChildNodes.Count > 0)
                {     //level 2
                    foreach (TreeNode subchild in node.ChildNodes)
                    {
                        if (value)
                            break;
                        if (subchild.Value == locId.ToString())
                        {
                            subchild.Selected = true;
                            value = true;
                            break;
                        }
                        else
                        {
                            if (subchild.ChildNodes.Count > 0)
                            { 
                                //level 3
                                foreach (TreeNode subchild1 in subchild.ChildNodes)
                                {
                                    if (value)
                                        break;
                                    if (subchild1.Value == locId.ToString())
                                    {
                                        subchild1.Selected = true;
                                        value = true;
                                        break;
                                    }
                                    else
                                    {
                                        if (subchild1.ChildNodes.Count > 0)
                                        {
                                            //level 4
                                            foreach (TreeNode subchild2 in subchild1.ChildNodes)
                                            {
                                                if (value)
                                                    break;
                                                if (subchild2.Value == locId.ToString())
                                                {
                                                    subchild2.Selected = true;
                                                    value = true;
                                                    break;
                                                }
                                                else
                                                {
                                                    if (subchild2.ChildNodes.Count > 0)
                                                    {
                                                        //level 5
                                                        foreach (TreeNode subchild3 in subchild2.ChildNodes)
                                                        {
                                                            if (value)
                                                                break;
                                                            if (subchild3.Value == locId.ToString())
                                                            {
                                                                subchild3.Selected = true;
                                                                value = true;
                                                                break;
                                                            }
                                                            else
                                                            {

                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }

This sample code search tree with multi root nodes: 此示例代码搜索树具有多个根节点:

// usage
SelectNodeByValue(treMain.Nodes, "1");

// recursive function:
protected void SelectNodeByValue(TreeNodeCollection Nodes, string ValueToSelect)
{
    foreach (TreeNode n in Nodes)
    {
        if (n.Value == ValueToSelect)
            n.Select();
        else if (n.ChildNodes.Count > 0)
            SelectNodeByValue(n.ChildNodes, ValueToSelect);
    }
}

Recursion is nice, but you are pushing and popping a lot of info on and of the stack. 递归很好,但是您正在堆栈中推送和弹出很多信息。 Not to mention The potential of being oblivious to whether or not you found what you were looking for. 更不用说遗忘您是否找到所需内容的潜力了。 When you find it you need to stop looking. 当您找到它时,您需要停止寻找。

    private void SelectNode(TreeNodeCollection nodes, string v)
    {
        Queue<TreeNode> queue = new Queue<TreeNode>();
        foreach(TreeNode node in nodes)
            queue.Enqueue(node);
        while(queue.Any())
        {
            TreeNode node = queue.Dequeue();
            if(node.Value == v)
            {
                node.Select();
                return;
            }
            foreach (TreeNode child in node.ChildNodes)
                queue.Enqueue(child);
        }
    }

and call it with 并用

    SelectNode(MyTreeView.Nodes, "MyValue");

This reduces the overhead of the recursive function quite a bit, and thus makes it much faster. 这样可以大大减少递归函数的开销,从而使其更快。 it still has a sudo recursive nature, but I hope it is easyer to read and understand then a true recursive function. 它仍然具有sudo递归性质,但是我希望它比真正的递归函数更容易阅读和理解。

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

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