TreeNode searching

2 posts, 0 answers
  1. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Jun 2007 Link to this post

    Requirements

    RadControls version

    6.2.1

    .NET version

    2.0

    Visual Studio version

    2005

    programming language

    C#

    browser support

    all browsers supported by RadControls


     
    PROJECT DESCRIPTION 
    This project shows how to search for a Treeview node using the node name entered through the TextBox or node name selected from the RadCombobox.

    ASPX:
    <body> 
        <form id="form1" runat="server">  
        <div> 
             <radA:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Height="200px" Width="534px">  
                <table> 
                    <tr> 
                        <td style="width: 122px">  
                            <table style="width: 344px">  
                                <tr> 
                                    <td colspan="2" style="height: 26px">  
                                        <strong><span style="color: #0099ff">Enter the NodeName to seach</span></strong></td> 
                                </tr> 
                                <tr> 
                                    <td style="width: 100px; height: 26px">  
                                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>  
                                    <td style="width: 100px; height: 26px">  
                                        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" /></td>  
                                </tr> 
                                <tr> 
                                    <td colspan="2" style="height: 26px">  
                                        <strong><span style="color: #990066">Or</span></strong></td> 
                                </tr> 
                                <tr> 
                                    <td colspan="2" style="height: 26px">  
                                        <span style="color: #0099cc"><strong>Select the code Name from Combo</strong></span></td> 
                                </tr> 
                                <tr> 
                                    <td colspan="2" style="height: 26px">  
                                        &nbsp;<radC:RadComboBox ID="RadComboBox1" runat="server" AllowCustomText="True"   
                                            MarkFirstMatch="True" AutoPostBack="True" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged" > 
                                        </radC:RadComboBox> 
                                    </td> 
                                </tr> 
                            </table> 
                        </td> 
                    </tr> 
                    <tr> 
                        <td style="width: 122px">  
                            <radT:RadTreeView ID="RadTreeView1" runat="server" MultipleSelect="True" Height="383px" Skin="Web20" Width="281px">  
                                <Nodes> 
                                    <radT:RadTreeNode runat="server" Text="Parent1" Value="Parent1">  
                                        <Nodes> 
                                            <radT:RadTreeNode runat="server" Text="Child1" Value="Child1">  
                                            </radT:RadTreeNode> 
                                            <radT:RadTreeNode runat="server" Text="Child2" Value="Child2">  
                                            </radT:RadTreeNode> 
                                            <radT:RadTreeNode runat="server" Text="Child3" Value="Child3">  
                                            </radT:RadTreeNode> 
                                        </Nodes> 
                                    </radT:RadTreeNode> 
                                    <radT:RadTreeNode runat="server" Text="Parent2">  
                                        <Nodes> 
                                            <radT:RadTreeNode runat="server" Text="Child4">  
                                            </radT:RadTreeNode> 
                                            <radT:RadTreeNode runat="server" Text="Child5">  
                                            </radT:RadTreeNode> 
                                        </Nodes> 
                                    </radT:RadTreeNode> 
                                    <radT:RadTreeNode runat="server" Text="Parent3">  
                                        <Nodes> 
                                            <radT:RadTreeNode runat="server" Text="Child6">  
                                            </radT:RadTreeNode> 
                                            <radT:RadTreeNode runat="server" Text="Child7">  
                                            </radT:RadTreeNode> 
                                        </Nodes> 
                                    </radT:RadTreeNode> 
                                </Nodes> 
                            </radT:RadTreeView> 
                        </td> 
                    </tr> 
                </table> 
            </radA:RadAjaxPanel>         
        </div> 
        </form> 
    </body> 

    //C#:
    using System;  
    using System.Data;  
    using System.Configuration;  
    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 Telerik.WebControls;  
    using System.Collections ;  
     
    public partial class _Default : System.Web.UI.Page   
    {  
        //Declare a global ArrayList itemsList    
        public static ArrayList itemsList;  
     
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                //initializing the ArrayList itemsList   
                itemsList = new ArrayList();  
                //Looping through each node in the Treeview collection  
                foreach (RadTreeNode node in RadTreeView1.Nodes)  
                {  
                    //Adding the node name to the arraylist  
                    itemsList.Add(node.Text);  
                    //Looping through the child nodes  
                    for (int i = 0; i < node.Nodes.Count; i++)  
                    {  
                        //Adding the child node to the arraylist  
                        itemsList.Add(node.Nodes[i].Text);  
                    }  
                }  
                //Populating the combobox  
                PopulateCombo();  
            }  
     
        }  
        //Function for populating the RadCombobox  
        private void PopulateCombo()  
        {  
            RadComboBox1.DataSource = itemsList;  
            RadComboBox1.DataBind();  
        }  
     
        //Button click event handler  
        protected void Button1_Click(object sender, EventArgs e)  
        {  
            //Getting the text entered in the textbox  
            string treeNode = TextBox1.Text;  
            //Passing the node name entered in the Textbox to the FindTreeNode function for finding the treenode  
            FindTreeNode(treeNode);  
        }  
        //FindTreeNode function  
        private void FindTreeNode(String treeNode)  
        {  
            //Looping through each node in the treeview  
            foreach (RadTreeNode node in RadTreeView1.Nodes)  
            {  
                //Checking the node name with the node name entered in the textbox  
                if (node.Text == treeNode)  
                {  
                    //if the node name equal to the node name entered in the textbox setting the selected property of the node to true  
                    node.Selected = true;  
                }  
                else  
                {                 
                    node.Selected = false;  
                }  
                //Looping through the child nodes   
                for (int i = 0; i < node.Nodes.Count; i++)  
                {  
                   if (node.Nodes[i].Text == treeNode)  
                    {  
                        //if the node name equal to the node name entered in the textbox setting the selected property of the node to true and expanding that node.  
                        node.Expanded = true;  
                        node.Nodes[i].Selected = true;  
                    }  
                    else  
                    {  
                        node.Nodes[i].Selected = false;  
                    }  
                }  
            }  
        }  
          
        protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)  
        {  
            //getting the selected item from the RadCombobox  
            String treeNode = RadComboBox1.SelectedItem.Text;  
            //Passing the selected item to the "FIndTreeNode" function.  
            FindTreeNode(treeNode);  
        }  
    }  
     

  2. Bryan
    Bryan avatar
    22 posts
    Member since:
    Jun 2012

    Posted 18 May 2011 Link to this post

    hi, i tried adding a node under a child node and it couldnt find it 

Back to Top