Jatinder Sharma
Jatinder Sharma

Reputation: 287

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

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

Upvotes: 1

Views: 11413

Answers (5)

user5419026
user5419026

Reputation: 181

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.

Upvotes: 0

user2965308
user2965308

Reputation: 71

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

Upvotes: 2

Zolfaghari
Zolfaghari

Reputation: 1323

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

Upvotes: 0

Jatinder Sharma
Jatinder Sharma

Reputation: 287

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
                                                            {

                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }

Upvotes: 1

SpiderCode
SpiderCode

Reputation: 10122

You can use :

node.Selected = true;

Have a look at the below code sample :

ASPX Page :

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

Upvotes: 1

Related Questions