2017-07-06 57 views
0

我有一个带有PopulateOnDemand选项的TreeView控件。每次触发TreeNodePopulate事件后,我都需要调用一个javascript函数。 我已经试过这在asp.net后运行javascript函数TreeView TreeNodePopulate事件

后面的代码:

protected void tvMyTree_PopulateNode(object sender, TreeNodeEventArgs e) 
{ 
    TreeManager.PopulateNodes(e.Node.Value);    
    ClientScript.RegisterStartupScript(this.GetType(),"ScriptInitButtons", "InitButtons();", true); 
} 

JS:

<script type="text/javascript"> 
    function InitButtons() { 
     $(".folder").button(); 
     $(".folder").click(function() { 
       createFolderDialog.data('parentID', $(this).attr('id')).dialog('open'); 
     }); 
     $(".leaf").button(); 
    } 
    </script> 

不过的RegisterStartupScript不工作。函数调用不会添加到页面中。

回答

0

我找到了解决方案here。但是,如果没有很小的修改,它不适用于我的ASP.NET版本。

该方法所做的是修改从服务器返回树数据后调用的函数。该函数被称为TreeView_ProcessNodeData,可在WebForms.js中找到。我必须检查此文件以查看其参数的名称。在这种情况下,签名是TreeView_ProcessNodeData(n,t)。知道确切的方法签名非常重要。

现在我们知道了正确的签名,我们创建了一个在文档准备就绪的函数。此查找将TreeView_ProcessNodeData函数作为字符串并提取方法体。然后在调用InitButtons函数的方法体的末尾附加一行。最后,它将TreeView_ProcessNodeData重新分配为一个接受参数'n'和't'的新函数。

从现在开始,当树视图调用TreeView_ProcessNodeData时,它会调用这个新函数,它的行为与原始类似,除了它在最后调用InitButtons。

<script type="text/javascript"> 

    $(document).ready(updateTreeViewProcessNodeData); 

    function updateTreeViewProcessNodeData() { 
     // convert the TreeView_ProcessNodeData to a string 
     var fnBody = TreeView_ProcessNodeData.toString(); 
     // remove everything before the first opening brace 
     fnBody = fnBody.substring(fnBody.indexOf("{") + 1); 
     // remove everything up to the last closing brace 
     fnBody = fnBody.substring(0, fnBody.length - 1); 
     // fnBody now contains the body of the method 
     // add a new line at the end of the method to call InitButtons 
     fnBody += "\nInitButtons();"; 
     // create a new function object from the text 
     // 'n' and 't' are the correct names of the function arguments expected by the method body 
     TreeView_ProcessNodeData = new Function("n", "t", fnBody); 
    } 

</script>