asp.net - How to find a tree node in a TreeView

Find Node in TreeView
asp.net treeview server control display hierarchical data. treeview is made up of nodes. we can add, remove or find tree node programmatically at run time. treeview control has sufficient built in mothods and properties to manage nodes. the following example source code demonstrate us how can we find specific tree node in treeview control.

FindNode method retrieve the TreeNode object in the treeview control at the specified value path. value path contains a delimiter separated list of node values that make a path by root node to current node. each node value path exists in his ValuePath property. PathSeparator property value specifies node separator delimiter character.
TreeViewFindNode.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if(!this.IsPostBack)
        {
            TreeView1.ForeColor = Color.LightGoldenrodYellow;
            TreeView1.BackColor = Color.HotPink;
            TreeView1.BorderWidth = 2;
            TreeView1.BorderColor = Color.DarkRed;
            TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
        }
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label1.Text = "";
        string nodePath = TextBox1.Text.ToString();
        try 
        {
            TreeView1.FindNode(nodePath).Checked = true;
            Label1.Text = "Node found and checked";
        }
        catch(Exception ex)
        {
            Label1.Text = "Node not found: " + nodePath;
            Label1.Text += "<br />Error: " + ex.ToString();
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to find tree node (TreeNode) in TreeView control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">TreeView Example: Find TreeNode</h2>
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
        <asp:Label 
             ID="Label1" 
             runat="server"
             ForeColor="DodgerBlue"
             Font-Italic="true"
             Font-Bold="true"
             Width="300"
             >
        </asp:Label>
        <br /><br />
        <asp:TreeView 
             ID="TreeView1"
             runat="server"
             DataSourceID="SiteMapDataSource1"
             Width="250"
             BorderWidth="2"
             BorderColor="Crimson"
             ShowCheckBoxes = "All"
             >
             <LeafNodeStyle BackColor="LightPink" ForeColor="Snow" />
        </asp:TreeView>
        <br />
        <asp:Label 
             ID="Label2"
             runat="server"
             Font-Bold="true"
             ForeColor="SaddleBrown"
             Text="Find Node"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1"
             runat="server"
             BackColor="LightGoldenrodYellow"
             ForeColor="SaddleBrown"
             Width="200"
             >
        </asp:TextBox>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server"
             ForeColor="SaddleBrown"
             Text="Find TreeNode"
             Height="45"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
    </div>
    </form>
</body>
</html>
Web.sitemap
Web.sitemap source code here.

   


More asp.net examples