Link to home
Start Free TrialLog in
Avatar of MKPanchal
MKPanchal

asked on

How to find child node in Treeview ASP.NET 2005

How to find the child node in the Treeview. I am using the Visual Studio 2005. I am able to find the Root Node by using the following, but how to find the child node.
        Dim name As TreeNode
        name = TreeView1.FindNode("Node2")
        If Not name Is Nothing Then

        End If
Avatar of archang3l
archang3l
Flag of United Arab Emirates image

Hello MKPanchal,

The childnodes of the rootnode can be enumerated by using the nodes property.
The first child is accessible through FirstNode.
The last child is accesible through LastNode.

        Dim name As TreeNode
        Dim node As TreeNode
        name = TreeView1.FindNode("Node2")
        If Not name Is Nothing Then
           name.FirstNode ' First Child node
           name.LastNode ' Last Child node
         
           ' Enumerate children
           For Each node In name.Nodes
             node.FullPath ' Get path of node
           Next node
        End If

For more info see:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.treenode.nodes(VS.71).aspx

Regards,

archang3l
Avatar of MKPanchal
MKPanchal

ASKER

Hello Archang3l
Thanks for the quick reply.
name.FirstNode , name.LastNode etc not a member of Treenode VS8. I am using the Visual Studio 2005 .Net Framework2.0
So in this some members removed.
Actually i am creating Treeview Dynamically which contains the Main Assembly Number (RootTree) and sub assembly Number(ChildTree). User enter the Serial number then i search same serial number in SQL database and find its Assembly Number. Now i want to find that assembly number exists in Treeview then want to mark as checked.
So i want to find the child node and then want to mark the checked.
Regards,
Mehul
The following code will enumerate all child nodes in a treeview recursively.

StringBuilder sb = new StringBuilder(string.Empty);
    private void EnumerateChildnodes()
    {
        foreach (TreeNode node in TreeView1.Nodes)
        {
            sb.Append("Node Text: " + node.Text);
            sb.Append("<br>");
            EnumerateChildnodes(node);
           
        }
        Response.Write(sb.ToString());
    }
    private void EnumerateChildnodes(TreeNode node)
    {
        foreach (TreeNode childnode in node.ChildNodes)
        {
            sb.Append("Node Text: " + childnode.Text);
            sb.Append("<br>");
            //childnode.ShowCheckBox = true;  
            //childnode.Checked = true;
            EnumerateChildnodes(childnode);
           
        }
    }
Here is the VB version.. place a treeview on a page and add childnodes .. use the following code in the codebehind to see the results.

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            EnumerateChildnodes()
        End If
    End Sub
    Dim sb As New StringBuilder
    Private Sub EnumerateChildnodes()
        For Each node As TreeNode In TreeView1.Nodes
            sb.Append("Node Text: " & node.Text)
            sb.Append("<br>")
            EnumerateChildnodes(node)
        Next
        Response.Write(sb.ToString())
    End Sub

    Private Sub EnumerateChildnodes(ByVal node As TreeNode)

        For Each childnode As TreeNode In node.ChildNodes

            sb.Append("Node Text: " & childnode.Text)
            sb.Append("<br>")
            EnumerateChildnodes(childnode)
        Next
    End Sub
Hi Codeclay,

Thanks for the same.
This code will make a string and in that i can know the Node name. Now i want to set the Checked property for that child node.

Regards,
Mehul

ASKER CERTIFIED SOLUTION
Avatar of codeclay
codeclay
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Codeclay,
Yes i found the answer. Thanks so much for your support.
Regards,
Mehul