在更新面板外部注入 JavaScript

发布于 2024-08-17 14:39:54 字数 822 浏览 2 评论 0原文

我有一个包含四个 UpdatePanel 的 aspx 页面。顺便说一下,每个 UpdatePanel 对应一个 JQuery UI 选项卡。我想要实现的是 UpdatePanel 外部的 JQuery UI 模式对话框,可以从任何 UpdatePanel 内部运行的服务器端代码调用该对话框。因此,第一个 UpdatePanel 内部是一个运行一些服务器端代码的 asp:Button。当发生错误时,我希望能够注入一些 JavaScript 来调用模式对话框来显示错误消息。这是我正在使用的代码:

Dim script As String = "showPopupMessage('{0}');"
script = String.Format(script, errorMessage)
ScriptManager.RegisterStartupScript(Me.UpdatePanelBizInfo, Me.UpdatePanelBizInfo.GetType, Guid.NewGuid.ToString, script, True)

页面上的 showPopupMessage 函数如下所示:

function showPopupMessage(msg) {
    $('#<%=Me.LabelPopupMessage.ClientID %>').text(msg);
    $('#dialogPopupMessage').dialog('open');
}

当代码运行以注入 JavaScript 时,没有任何反应。我假设这与 UpdatePanel 内运行的代码中发生错误有关。检查生成的 HTML 后,就会发现 JavaScript 就在那里。我做错了什么?

I have an aspx page with four UpdatePanels. Incidentally, each UpdatePanel corresponds to a JQuery UI tab. What I am trying to achieve is a JQuery UI modal dialog OUTSIDE the UpdatePanels that can be called from server-side code running INSIDE any of the UpdatePanels. So, inside the first UpdatePanel is an asp:Button which runs some server-side code. When an error ocurrs, I want to be able to inject some JavaScript that will call the modal dialog to display the error message. Here is the code I am using:

Dim script As String = "showPopupMessage('{0}');"
script = String.Format(script, errorMessage)
ScriptManager.RegisterStartupScript(Me.UpdatePanelBizInfo, Me.UpdatePanelBizInfo.GetType, Guid.NewGuid.ToString, script, True)

The showPopupMessage function on the page looks like this:

function showPopupMessage(msg) {
    $('#<%=Me.LabelPopupMessage.ClientID %>').text(msg);
    $('#dialogPopupMessage').dialog('open');
}

When the code runs to inject the JavaScript, nothing happens. I am assuming it has something to do with the fact that the error ocurrs in the code running inside an UpdatePanel. Upon inspecting the resulting HTML, the JavaScript is there. What am I doing wrong?

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

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

发布评论

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

评论(1

嘿看小鸭子会跑 2024-08-24 14:39:54

您可以在异步回发期间在页面上运行 javascript,但要使其正确执行,您需要使用 ScriptManager 类来注册它。一旦你这样做了,当更新面板回调时,它将作为启动脚本触发。在这些情况下,您可以打开模态窗口并调用 fire 作为启动脚本。

这是一个非常相似的问题,我在其中发布了一些自定义代码(请参阅接受的答案)。它可能会解决你的问题。

ASP.Net - AJAX UpdatePanel 内的 Javascript

如果本身您模态打开调用是 openMyModal(someParams...);,您可以将链接代码用作:

string scriptText = "openMyModal(someParams...);";
this.RegisterClientStartupScript("openMyModal", scriptText);

You can run javascript on the page during an asynchronous postback, but to have it execute properly you need to use the ScriptManager class to register it. Once you do that it will fire as a startup script when the update panels callback. You could have the modal window open call fire as a startup script in those cases.

Here's a very similar question where I posted some custom code (see accepted answer). It'll likely solve your issue.

ASP.Net - Javascript inside AJAX UpdatePanel

If per se your modal open call is openMyModal(someParams...);, you can use the linked code as:

string scriptText = "openMyModal(someParams...);";
this.RegisterClientStartupScript("openMyModal", scriptText);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文