0

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

后面的代码:

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 不起作用。函数调用未添加到页面中。

4

1 回答 1

0

我在这里找到了解决方案。但是,如果没有进行微小的修改,它就不适用于我的 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>
于 2017-09-01T12:15:26.723 回答