是否可以在 UpdatePanel 中重新执行 javascript?
我有以下内容:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哟!有一种简单的方法可以让 JavaScript 再次运行。将 javascript 放在页面的 HeadContent 部分:
然后在更新面板内的页面上添加部分,调用您为 javascript 创建的函数:
在我正在处理的一个项目中,我想要所有 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:
then on your page inside the update panel add portion with a call up to the function that you created for your javascript:
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
由于 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.
如果 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.