(This tutorial assumes you have script debugging enabled in your web browser. It does not contain information on how to enable this.)
The Web TreeView control uses javascript on the client side to help control the tree. Unfortunately, this script is injected behind the scenes and is not visible in the source of the webpage. You can, however, use this simple trick to step through the injected javascript and examine the TreeView's objects during runtime. Note: You can use this technique to examine any aspnet control's javascript; I'm concentrating this on the TreeView because I have been working with it the most.
In the Page_Load event handler of the webpage that contains a TreeView, add the following (C#) code:
Page.ClientScript.RegisterStartupScript(this.GetType(), "Debug", "Break();", true);
This injects a line of javascript into the same area that the TreeView and all other controls inject its code. Next, create the Break() method in a javascript file. Right click on your web project and select Add New Item..., select JScript File, and call this file "sneaky.js". It will then be added to the root of your web project. Open this file and add the following code:
function Break(){ alert("BREAK!");}
Set a breakpoint at this location in the js file. When this code is reached at runtime, we want to break into the execution and step through it. After this, we'll step into the code that has been injected by the webpage controls. That's where we'll want to be.
Lastly, open the webpage that has the TreeView. In the Head section, add the following line:
<script type="text/javascript" src=sneaky.js></script>
Run your project. You will see the webpage load, then your function Break() will execute, hitting the breakpoint and stopping execution. Step through the code (F10) past the exit of the Break() function. You will step out of sneaky.js and into the aspx file for your webpage. Only now there will be a bunch of javascript code that has been injected by your webpage controls.
All data about your treeview is kept in an object that is named "[TreeView Name]_Data", where [TreeView Name] is the name you gave your TreeView. I.e., if you called your TreeView "tvNavigation", the server-side object would be called "tvNavigation_Data".
You can right-click on this object and examine it. You can use this object to control the TreeView from the client side via javascript. For instance, if you wanted to scroll the currently selected node into view, you could easily get the anchor element that represents the currently selected node via the following code:
var nodeName = Document.tvNavigation_Data.selectedNodeID.value;
var selectedNode = document.all ? document.all[nodeName] : document.getElementById(nodeName);
if(selectedNode){ /* Your scrolling code goes here */ }
A note about scrolling: The anchor node is contained in a table; if you are using the anchor's offsetTop to determine how far you have to scroll, you'll always get 0 as the result. To get the containing table element (which will have a valid offsetTop value for this purpose), add the following code after getting the anchor element and before performing any scrolling calculations:
selectedNode = selectedNode.parentNode.parentNode.parentNode.parentNode
I need to add node through javascript in tree control using ASP.net in VS2005 so that node added at client side to avoid postback during addition of node , please help me how we do that.