gpt4 book ai didi

javascript - RegisterClientScriptCode 在部分回发后不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 05:26:00 25 4
gpt4 key购买 nike

以下代码行位于 SharePoint 网站的用户控件中。

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "jquery144", "<script type=\"text/javascript\" src=\"/_layouts/Unicre.Web.RUOnline.Controlos/Scripts/jquery-1.4.4.min.js\"></script>", false);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "javascriptgeral", "<script type=\"text/javascript\" src=\"/_layouts/Unicre.Web.RUOnline.Controlos/Scripts/javascript.js\"></script>", false);

为什么不起作用?我也无法在响应中生成的 HTML 中找到相应的脚本标记。

问题与更新面板有关。部分回发后,Javascript 停止工作。它不应该与上面的代码一起工作吗?

(我也尝试了 RegisterClientScriptInclude 方法,但结果相同。)

最佳答案

这不适用于部分回发。您需要在 Page_Load 上注册脚本.在用户控件中,附加到 Load 事件并从该处理程序调用 ScriptManager.RegisterClientScriptBlock()。

After the partial postback, the Javascript stops working

UpdatePanel 部分回发是 UpdatePanel 的 DOM 更新 <div>内容。这意味着以前的内容丢失了,因此 <div> 中包含的内联脚本的状态失去它的状态。

参见 here了解更多信息:

UpdatePanel does its work on the client through the innerHTML DOM property. A delta is retrieved from the server, finds itself in the existing DOM, disposes of the contents, and then assigns the new content via innerHTML. ... But inline script doesn't work this way. Setting the innerHTML of a DOM element to HTML which contains a script block does not cause that script to execute.

同样,在 UpdatePanel 更新时调用 ScriptManager.RegisterClientScriptBlock() 不会像页面加载那样工作。您可以添加 <script>元素直接添加到 UpdatePanel 内容,但它不会执行。

更新

您可以在 adding an endRequest handler to the PageRequestManager 部分回发后调用 javascript 代码:

<script>
function load() {
//register the handler
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
}
//this handler will execute after a partial postback
function EndRequestHandler(){
//...arbitrary code...
}
window.onload = load;
</script>

关于javascript - RegisterClientScriptCode 在部分回发后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6652024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com