I had to struggle a little with a problem in using ASP.NET TreeView, which I had customized for one of the projects. The issue was to execute some client side script, after the TreeView has completed loading Nodes asynchronously. However, as I found out - unlike other controls TreeView does not provide a ready mechanism to hook custom code after the nodes are loaded asynchronously (using PopulateOnDemand property)
Problem description:
When we use ASP .NET TreeView control, it provides us an option of lazy loading or asynchronous loading of Tree Nodes. This is taken care of by a whole lot of JavaScript that are added to the page by adding reference to couple of WebResource.axd files:
a. First script file contains Tree View html updation scripts and
b. the other script file contains Async postback related scripts
When a node is marked for loading its contents Asynchronously (using node.PopulateOnDemand = true), a JavaScript method - TreeNode_PopulateOnDemand is called on expand icon click.
This internally calls the Async Page load methods. And finally gives the content to be updated to another method - TreeNode_ProcessNodeData.
Solution:
The fix that I worked out was to modify the TreeNode_ProcessNodeData function itself, to add my custom method once it completes its processing. Here is the code snippet to modify the TreeView_ProcessNodeData:
function TreeView_UpdateProcessNodeData(){
var fnStmt = TreeView_ProcessNodeData.toString();
fnStmt = fnStmt.substring(fnStmt.indexOf("{") + 1);
fnStmt = fnStmt.substring(0, fnStmt.length-1);
fnStmt += "\nMyCustomMethod();";
TreeView_ProcessNodeData = new Function("result", "context", fnStmt);
} |