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

时间:2014-01-03 13:04:49

标签: c# asp.net

如何在aspx树视图中按值设置所选节点我尝试了以下代码,但这是在树的第一级工作

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

我也试过这个

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

但没有工作。

我想要像我们在下拉菜单中设置的那样

dropdown1.selectedValue="5";

请帮帮我

5 个答案:

答案 0 :(得分:2)

一个很好的递归函数可以解决这个问题:

<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);
        }
    }
}

答案 1 :(得分:1)

您可以使用:

  

node.Selected = true;

看一下下面的代码示例:

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>

更新 代码背后:

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;
    }
}

答案 2 :(得分:1)

我试过这对我来说在树视图中有五个级别可以告诉我任何安慰方式吗

 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
                                                            {

                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }

答案 3 :(得分:0)

此多根节点的示例代码搜索树:

// 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);
    }
}

答案 4 :(得分:0)

递归很好,但是您正在堆栈中推送和弹出很多信息。更不用说遗忘您是否找到所需内容的潜力了。找到后,您需要停止寻找。

    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);
        }
    }

并用

调用
    SelectNode(MyTreeView.Nodes, "MyValue");

这大大减少了递归函数的开销,从而使其更快。它仍然具有sudo递归性质,但是我希望它比真正的递归函数更容易阅读和理解。