ASP.NET UpdatePanel:如何让客户端脚本在每次刷新时运行

发布于 2024-12-12 21:32:14 字数 627 浏览 0 评论 0原文

我有一个自制的工具栏控件,可在多个地方使用,包括在 UpdatePanel 中和外部。它依赖于一些 jQuery 来设置其按钮。它在其程序集中嵌入了自己的启动脚本。

我遇到的问题是,当工具栏位于更新面板中时,如何让启动脚本在正确的时间运行。由于它是在更新面板标记中的隐藏面板中声明的,因此它会在创建更新面板时创建并加载。但直到用户完成一些操作并且隐藏面板变得可见之后,它才会真正被渲染。

我希望启动脚本在每次更新面板刷新时运行。 (理想情况下,我希望它仅在工具栏可见的情况下才执行此操作,但如果必须的话,我将进行“每次刷新”。)但我希望工具栏在更新面板之外时仍然可以运行,当它需要的时候要做的就是挂钩文档的 .ready() 处理程序。

鉴于工具栏无法从服务器端判断(据我所知)它是否位于更新面板内,但负责确保必要的 JavaScript,我该如何实现这一点?

谢谢。

ETA:我在 这个问题。这适用于我的工具栏,无论它是在更新面板中还是在更新面板之外。谢谢大家!

I have a homemade toolbar control that is used in several places, both in an UpdatePanel and outside of one. It relies on some jQuery to set up its buttons. It has its own startup script embedded in its assembly.

What I'm having trouble with is getting the startup script to run at the right time when the toolbar is in the update panel. Since it's declared in a hidden panel in the update panel's markup, it gets created and loaded when the update panel is created. But it doesn't actually get rendered until the user has done a couple of things and the hidden panel is made visible.

What I would like is for the startup script to run every time the update panel refreshes. (Ideally I'd like it to only do that if the toolbar is visible, but I'll take "every refresh" if I have to.) But I want the toolbar to still function when outside an update panel, when all it needs to do is hook into the document's .ready() handler.

Given that the toolbar can't tell (as far as I know) from the server side whether it's inside an update panel or not, but is responsible for ensuring the necessary javascript, how can I make this happen?

Thanks.

ETA: I found an answer in the answer to this question. This works for my toolbar whether it's in the update panel or outside of one. Thanks, everybody!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-12-19 21:32:14

如果我错了,请纠正我,但我认为每次更新 updatepanel 时都应该执行 UpdatePanel 中的 jQuery 工具栏。所以也许在服务器端你可以判断它是否是异步回发。如果是,那么您可以在将要执行的更新面板中编写一些 JavaScript。您可以使用加载到更新面板内的占位符中的文字控件。像...

public void WebForm1_OnLoad(Object sender, EventArgs args){


    if(ScriptManager.GetCurrent(Page).IsInAsyncPostBack){
        var con = new LiteralControl("INSERT javascript here that would call jquery method");
        this.placeHolder.Controls.Add(con);
    }
}

Correct me if I'm wrong but I would think that the jQuery toolbar within the UpdatePanel should be executed everytime that the updatepanel is updated. So maybe on the server side you can tell whether it's a asyncpostback. If it is then you can use write some javascript within the update panel that will be executed. You might use a literalcontrol that you load into a placeholder within the update panel. Something like...

public void WebForm1_OnLoad(Object sender, EventArgs args){


    if(ScriptManager.GetCurrent(Page).IsInAsyncPostBack){
        var con = new LiteralControl("INSERT javascript here that would call jquery method");
        this.placeHolder.Controls.Add(con);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文