是否可以在 UpdatePanel 中重新执行 javascript?

发布于 2024-10-07 00:55:37 字数 372 浏览 0 评论 0原文

我有以下内容:

<asp:UpdatePanel ID="upd" runat="server">
    <ContentTemplate>

        <script> alert("execute again"); </script>

        <asp:LinkButton ID="go" runat="server" Text="Exec JS" />

    </ContentTemplate>
</asp:UpdatePanel>

页面第一次呈现时执行脚本。如果我单击按钮导致回发,它不会再次执行。

有没有办法让它再次执行脚本?

I have the following:

<asp:UpdatePanel ID="upd" runat="server">
    <ContentTemplate>

        <script> alert("execute again"); </script>

        <asp:LinkButton ID="go" runat="server" Text="Exec JS" />

    </ContentTemplate>
</asp:UpdatePanel>

The first time the page renders the script is executed. If I click the button to cause a postback it doesn't execute again.

Is there a way to make it execute the scripts again?

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

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

发布评论

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

评论(3

熟人话多 2024-10-14 00:55:37

哟!有一种简单的方法可以让 JavaScript 再次运行。将 javascript 放在页面的 HeadContent 部分:

<script> 

function BindIt()
{
    alert("execute again"); 
}

</script>

然后在更新面板内的页面上添加部分,调用您为 javascript 创建的函数:

            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                <script type="text/javascript">
                    Sys.Application.add_load(BindIt);
                </script>
...

在我正在处理的一个项目中,我想要所有 javascript在页面上再次运行,所以我只是将其全部放入一个巨大的 BindIt 样式函数中。我花了一段时间才弄清楚,我尝试了很多方法来让它工作,但这是我能够让 jquery 与 updatepanels 一起工作的唯一方法

Yo! There's an easy way to get the JavaScript to run again. Put the javascript on the HeadContent portion of your page:

<script> 

function BindIt()
{
    alert("execute again"); 
}

</script>

then on your page inside the update panel add portion with a call up to the function that you created for your javascript:

            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                <script type="text/javascript">
                    Sys.Application.add_load(BindIt);
                </script>
...

In a project that I was working on, I wanted all of the javascript on the page to run again, so I just put it all into a massive BindIt style function. It took me a while to figure it out and I tried many methods to get this to work but this was the only way I was ever able to get jquery to work with updatepanels

熊抱啵儿 2024-10-14 00:55:37

由于 ASP 只是推送原始数据,而不是实际执行它,因此您可能需要使用 ScriptManager 的 RegisterStartupScript 方法以编程方式注入脚本以在部分回发后加载。

编辑
更直接地,尝试查看 ScriptManager.RegisterStartupScript() 中的 UpdatePanel。例如

Since ASP is just pushing the raw data, not actually executing it you'll need to probably use the RegisterStartupScript method of the ScriptManager to programmatically inject the script to load after a partial postback.

EDIT
More directly, try looking in to ScriptManager.RegisterStartupScript() for UpdatePanels. e.g.

顾北清歌寒 2024-10-14 00:55:37

如果 JavaScript 与 jQuery 相关,以下链接肯定会有所帮助:

http://weblogs.asp.net/hajan/archive/2010/10/07/make-them-to- love-each-other-asp-net-ajax-updatepanels-amp-javascript-jquery-functions.aspx

我个人使用过本文中提到的第二种方法,它对我来说效果很好。

If the JavaScript is jQuery related, following link would definitely help:

http://weblogs.asp.net/hajan/archive/2010/10/07/make-them-to-love-each-other-asp-net-ajax-updatepanels-amp-javascript-jquery-functions.aspx

I have personally used the 2nd method mentioned in this article, and it worked for me properly.

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