1

I am looking to find a way to just simply know the selected node in TreeView using JavaScript. Suppose there are n number of nodes in the parent-child relationship, then what I want to get value of the selected node in JavaScript so that I can manipulate and work on the values selected in JavaScript rather than do a full page postback to get the selected tree node as selected by user in ASP.Net.

Is there any alternative to know the Node and whether the Node has any child or parent in JavaScript?

Here is my example which i am using to create and populate TreeView:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TreeNode t_Node;
            using (OleDbConnection con = new OleDbConnection())
            {
                using (DataSet t_DS = new DataSet())
                {
                    using (OleDbCommand myCommand = new OleDbCommand())
                    {
                        OleDbDataAdapter t_DA;
                        con.ConnectionString = "Provider=SQLOLEDB;Data Source = .; Initial Catalog = NorthWind; User ID = sa; Password = ";
                        myCommand.CommandText = "select EmployeeID, FirstName  + ' ' + LastName As Name from Employees Order by EmployeeID";
                        myCommand.Connection = con;
                        try
                        {
                            con.Open();
                            t_DA = new OleDbDataAdapter(myCommand);
                            t_DA.Fill(t_DS);
                            foreach (DataRow t_DR in t_DS.Tables[0].Rows)
                            {
                                t_Node = new TreeNode(t_DR["Name"].ToString(), t_DR["EmployeeID"].ToString());
                                TreeView1.Nodes.Add(t_Node);
                            }

                        }
                        catch (Exception ex)
                        {
                            Response.Write(String.Format("There is an error{0}", ex));
                        }
                        finally
                        {
                            con.Close();
                        }
                    }
                }
            }
        }
    }

1 Answer 1

1

You'll have an object called {TreeView name}_Data. All the juicy parts are in there. To get the selected node, you want the selectedNodeID property. For example, if you have a TreeView called Products, then try this:

var selectedItem = Products_Data.selectedNodeID.value;
var selectedNode = Document.getElementById(selectedItem);
Sign up to request clarification or add additional context in comments.

2 Comments

Hello Friend Thanks for your comment, let's say i am creating and populating my treeview as follows in the example below, please let me know how can i get the EmployeeID from the selected node in the TreeView. here follows the example. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack){ } BindTreeParent(); }
sorry for my previous comment without the example, here i donot know how to add code in this comment, hence i edited the above question to include the Code example for creating the TreeNode. now the problem is that i want the EmployeeID as selected by user from the TreeView using javascript. how to get that value. kindly please help me in this. waiting for replies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.