2017-07-06 56 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>