Click here to Skip to main content
13,256,323 members (52,212 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 25 Feb 2010

Apply Search On the nodes of an Treeview ASP.NET control

Rate this:
Please Sign up or sign in to vote.
Hello All,Today I am showing an interesting block of code which will help you in applying user search on a tree view control of applications.PrerequisitesFor this you have to create an demo project using language C#Action:Step 1: Now Create a web page with name...
Hello All,
Today I am showing an interesting block of code which will help you in applying user search on a tree view control of applications.

For this you have to create an demo project using language C#

Step 1: Now Create a web page with name TreeViewS.aspx .
Step 2: Now Paste the below code there.
//The Search Page code (Design of page)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeViewS.aspx.cs" Inherits="webUI_TreeViewS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
    <title>Abhishek TreeView FindNode</title>
    <form id="form1" runat="server">
            TreeView FindNode Using User Input</h3>
        <strong>Search Criteria:</strong>(Use a forward slash (/) to delimit each node value.)<br />
        <asp:TextBox ID="ValueText" runat="server" Text="Table/Example One/Section 1.0"
        <br />
        <br />
        <asp:Button ID="Submit" runat="server" OnClick="Button_Click" Text="Find Node" />
        <br />
        <br />
        <asp:TreeView ID="MainTree" runat="server" Font-Names="Arial" ForeColor="Blue" PathSeparator="/">
                <asp:TreeNodeStyle ChildNodesPadding="10" Font-Bold="true" Font-Size="12pt" ForeColor="DarkGreen" />
                <asp:TreeNodeStyle ChildNodesPadding="5" Font-Bold="true" Font-Size="10pt" />
                <asp:TreeNodeStyle ChildNodesPadding="5" Font-Size="10pt" Font-Underline="true" />
                <asp:TreeNodeStyle ChildNodesPadding="10" Font-Size="8pt" />
                <asp:TreeNode SelectAction="None" Text="Table">
                    <asp:TreeNode Text="Example One" Value="Example One">
                        <asp:TreeNode Text="Section 1.0" Value="Section 1.0">
                            <asp:TreeNode Text="Topic 1.0.1" Value="Topic 1.0.1"></asp:TreeNode>
                            <asp:TreeNode Text="Topic 1.0.2" Value="Topic 1.0.2"></asp:TreeNode>
                            <asp:TreeNode Text="Topic 1.0.3" Value="Topic 1.0.3"></asp:TreeNode>
                        <asp:TreeNode Text="Section 1.1">
                            <asp:TreeNode Text="Topic 1.1.1" Value="Topic 1.1.1"></asp:TreeNode>
                            <asp:TreeNode Text="Topic 1.1.2" Value="Topic 1.1.2"></asp:TreeNode>
                            <asp:TreeNode Text="Topic 1.1.3" Value="Topic 1.1.3"></asp:TreeNode>
                            <asp:TreeNode Text="Topic 1.1.4" Value="Topic 1.1.4"></asp:TreeNode>
                    <asp:TreeNode Text="Example Two" Value="Example Two">
                        <asp:TreeNode Text="Section 2.0" Value="Section 2.0">
                            <asp:TreeNode Text="Topic 2.0.1" Value="Topic 2.0.1"></asp:TreeNode>
                            <asp:TreeNode Text="Topic 2.0.2" Value="Topic 2.0.2"></asp:TreeNode>

Step 3: Now paste the below C# code in your web page code file (.CS) file.
//C# Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
public partial class webUI_TreeViewS : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        //This Code is for highlight the selected node
        if (!IsPostBack)
            MainTree.SelectedNodeStyle.ForeColor = Color.Red;
    protected void Button_Click(object sender, EventArgs e)
            TreeNode node = MainTree.FindNode(Server.HtmlEncode(ValueText.Text.Trim()));
            if (node != null)
                // Indicate that the node was found.
                ValueText.Text = "";
                // Indicate that the node is not in the TreeView control.
                ValueText.Text = "The specified node (" + ValueText.Text + ") is not in this TreeView control.";
        catch (Exception ex)
            throw ex;

Now set this page as start page , then compile and run the project.
Now Type the name of node for search the node will be selected autometicaly in tree list.

Have Fun :laugh:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Presently Abhishek is working as an Senior Software Developer in Youngtech Pvt. Ltd.(Division of Youngsoft Inc.).Abhishek have 9 years hands on and various database handling experiences. Presently he is engage in creation of an application to handle online database services.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 26 Feb 2010
Article Copyright 2010 by Abhishek Banerjee (A Web Developer)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid