运行 javascript 函数在 asp.net TreeView TreeNodePopulate 事件之后
Run javascript function after asp.net TreeView TreeNodePopulate event
我有一个带有 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 不工作。函数调用未添加到页面中。
我找到了解决方案 here。但是,如果不进行微小的修改,它就无法与我的 ASP.NET 版本一起使用。
该方法的作用是修改服务器返回树数据后调用的函数。该函数称为 TreeView_ProcessNodeData,可在 WebForms.js 中找到。我不得不检查这个文件以查看其参数的名称。在这种情况下,签名是 TreeView_ProcessNodeData(n,t)。了解确切的方法签名很重要。
现在我们知道了正确的签名,我们创建了一个在文档就绪时运行的函数。这发现 gets 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>
我有一个带有 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 不工作。函数调用未添加到页面中。
我找到了解决方案 here。但是,如果不进行微小的修改,它就无法与我的 ASP.NET 版本一起使用。
该方法的作用是修改服务器返回树数据后调用的函数。该函数称为 TreeView_ProcessNodeData,可在 WebForms.js 中找到。我不得不检查这个文件以查看其参数的名称。在这种情况下,签名是 TreeView_ProcessNodeData(n,t)。了解确切的方法签名很重要。
现在我们知道了正确的签名,我们创建了一个在文档就绪时运行的函数。这发现 gets 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>