ASP.NET TreeView - after Async Load

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);
}
This article is part of the GWB Archives. Original Author: prash

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.