THIS IS A ASP.NET Web APPLICATION NOT WINDOWS FORMS
Problem/ Question: To search a child node anywhere in the treeview. I need to type a Node name in the textbox and search the node name in the Treeview and highlight the node name on finding. I don’t how to do it. I tried for sometime but didn’t find the solutions.So, can any body give some idea ?
FYI : I have created a ASP.NET Web application treeview to populate Parent nodes and corresponding child nodes for each Parent Node. The user can add any number of child nodes in the treeview.
Given below is the code that I have done till now for populating the treeview for any number of childs and child levels.
Code behind file :

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)// Checks if the page is loaded first time
        {
            PopulateRootLevel(); // Populates the root or parent 
        }        
    }
    public void PopulateRootLevel()
    {
        SqlConnection objConn = new SqlConnection(@"");
        SqlCommand objCommand = new SqlCommand(@"select dp.CustomerName,dh.RowId,(select count(*) FROM DistributorHierarchy WHERE ParentID=dh.RowId) childnodecount FROM DistributorHierarchy dh INNER JOIN DistributorProfile dp ON dh.CustomerNumber = dp.CustomerNumber where dh.ParentID = 0 ", objConn);
        SqlDataAdapter da = new SqlDataAdapter(objCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PopulateNodes(dt, TreeView1.Nodes);                 
    }
    public void PopulateSubLevel(int ParentID, TreeNode parentNode)
    {
        SqlConnection objConn = new SqlConnection();
        SqlCommand objCommand = new SqlCommand(@"select dp.CustomerName,dh.RowId,(select count(*) FROM DistributorHierarchy WHERE ParentID=dh.RowId) childnodecount  FROM DistributorHierarchy dh INNER JOIN DistributorProfile dp ON dh.CustomerNumber = dp.CustomerNumber where dh.ParentID=@ParentID", objConn);
        objCommand.Parameters.Add("@ParentID", SqlDbType.Int).Value = ParentID;
        SqlDataAdapter da = new SqlDataAdapter(objCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PopulateNodes(dt, parentNode.ChildNodes);
    }
    public void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
    }
    public void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
    {
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode tn = new TreeNode();
                tn.Text = dr["CustomerName"].ToString();
                tn.Value = dr["RowId"].ToString();
                nodes.Add(tn);
                tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
            }
    }    

}

Dani AI

Generated

Two quick facts up front: your TreeView is lazy-loaded (you set nodes to populate on demand), so nodes that haven’t been expanded do not exist on the page and a pure client-side DOM search will miss them. Use a server-side lookup to find the node’s ID/path and then expand/load that path on the server, or only use a client search if the whole tree is already rendered. (learn.microsoft.com)

Recommended, reliable approach (server-side)

  1. Search the database for the node(s) that match the textbox (e.g. WHERE CustomerName LIKE ...). Retrieve the matched RowId and the chain of parent IDs up to the root (either with an iterative loop or a recursive CTE in SQL).
  2. Using that parent chain (root → … → target) expand and populate each level so the target node actually exists in TreeView.Nodes. Call your existing populate routine for each parent level as you walk the chain. You can then locate the node and programmatically select it. The TreeView supports finding by a ValuePath (PathSeparator) and also a programmatic Select() on a node. (learn.microsoft.com)

Example helper (conceptual C# — adapt to your methods)

private TreeNode FindNodeByValue(string value, TreeNodeCollection nodes)
{
    foreach (TreeNode n in nodes)
    {
        if (n.Value == value) return n;
        var found = FindNodeByValue(value, n.ChildNodes);
        if (found != null) return found;
    }
    return null;
}

private void ExpandPathAndSelect(List<string> path)
{
    TreeNode current = null;
    foreach (var id in path) {
        var scope = (current == null) ? TreeView1.Nodes : current.ChildNodes;
        var node = FindNodeByValue(id, scope);
        if (node == null) {
            // load children for current (use your PopulateRootLevel/PopulateSubLevel here)
            if (current == null) PopulateRootLevel();
            else PopulateSubLevel(int.Parse(current.Value), current);
            node = FindNodeByValue(id, scope);
        }
        if (node == null) return; // not found
        node.Expand();
        current = node;
    }
    current?.Select(); // highlights according to TreeView SelectedNodeStyle
}

Styling and UX notes

  • Use TreeView.SelectedNodeStyle (or level styles/CSS classes) to control the highlight rather than injecting HTML into node text. That keeps markup safe and consistent. (learn.microsoft.com)
  • If multiple matches are possible, return a small list to the user (or highlight the first and offer “next” behavior).
  • If you prefer client-only search, do it only when you know the whole tree was preloaded; otherwise the server-path method above is the correct solution. ’s client-side pointer is useful in the fully-loaded case but won’t find lazy nodes.

THIS IS A ASP.NET Web APPLICATION NOT WINDOWS FORMS
Problem/ Question: To search a child node anywhere in the treeview. I need to type a Node name in the textbox and search the node name in the Treeview and highlight the node name on finding. I don’t how to do it. I tried for sometime but didn’t find the solutions.So, can any body give some idea ?
FYI : I have created a ASP.NET Web application treeview to populate Parent nodes and corresponding child nodes for each Parent Node. The user can add any number of child nodes in the treeview.
Given below is the code that I have done till now for populating the treeview for any number of childs and child levels.
Code behind file :

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)// Checks if the page is loaded first time
        {
            PopulateRootLevel(); // Populates the root or parent 
        }        
    }
public void PopulateRootLevel()
{
SqlConnection objConn = new SqlConnection(@"");
SqlCommand objCommand = new SqlCommand(@"select dp.CustomerName,dh.RowId,(select count(*) FROM DistributorHierarchy WHERE ParentID=dh.RowId) childnodecount FROM DistributorHierarchy dh INNER JOIN DistributorProfile dp ON dh.CustomerNumber = dp.CustomerNumber where dh.ParentID = 0 ", objConn);
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);                 
}
public void PopulateSubLevel(int ParentID, TreeNode parentNode)
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCommand = new SqlCommand(@"select dp.CustomerName,dh.RowId,(select count(*) FROM DistributorHierarchy WHERE ParentID=dh.RowId) childnodecount  FROM DistributorHierarchy dh INNER JOIN DistributorProfile dp ON dh.CustomerNumber = dp.CustomerNumber where dh.ParentID=@ParentID", objConn);
objCommand.Parameters.Add("@ParentID", SqlDbType.Int).Value = ParentID;
SqlDataAdapter da = new SqlDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
}
public void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
}
public void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
        foreach (DataRow dr in dt.Rows)
        {
            TreeNode tn = new TreeNode();
            tn.Text = dr["CustomerName"].ToString();
            tn.Value = dr["RowId"].ToString();
            nodes.Add(tn);
            tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
        }
}    

} 

end quote.

Hello,

First of all please use code block to post some codes.

You can have treenode text/value at client side.

please check out below link :

Get Treenode value client side

Though it's not exactly what you're looking for but it may help you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.