从用户控件注入的JavaScript的UpdatePanel内 [英] Injecting javascript from a user control inside an UpdatePanel
问题描述
在页面呈现最初的 initializeControl
函数被调用,一切工作。
When the page initially renders, the initializeControl
function is called and everything works.
在页面执行完全后回(通过提交按钮),在 initializeControl
功能也被称为和一切正常。
When the page performs a full post-back (via a submit button), the initializeControl
function is also called and everything works.
在还有就是的UpdatePanel
部分后回,不过, initializeControl
函数不会被调用和控制停止工作。
When there is partial post-back of the UpdatePanel
, though, the initializeControl
function is never called and the control stops working.
HTML
<asp:ScriptManager ID="myScriptManager" runat="server" />
<asp:UpdatePanel ID="myUpdatePanel" runat="server">
<ContentTemplate>
<uc:MyControl ID="myControl" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
用户控制:
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim initializeScript = String.Format("initializeControl('{0}');", ClientID)
Page.ClientScript.RegisterStartupScript(GetType(Page),
New Guid().ToString(), initializeScript, True)
MyBase.Render(writer)
End Sub
有关测试的目的,假设 initializeControl
功能仅仅是一个调试;
(函数的内容工作正常 - 它只是没有被当它需要被调用)
For testing purposes, assume the initializeControl
function is just a debugger;
(the content of the function works fine - it's just not being called when it needs to be).
请记住,没有办法(据我所知)的用户控件知道这是否是内部的的UpdatePanel
或没有了,也有没有办法它可以在服务器code父页面上访问的ScriptManager
元素。
Please keep in mind that there is no way (that I know of) for the user control to know if it is inside an UpdatePanel
or not, and there is also no way it can access the ScriptManager
element on the parent page in server code.
先谢谢了。
P.S。我知道的UpdatePanel
是可怕的,应不惜一切代价避免的,但我有数百消费那些已经使用这些网页的工作,他们不能被改变。
P.S. I know UpdatePanel
s are awful and should be avoided at all costs, but I am working with hundreds of consuming pages that are already using them and they can not be changed.
推荐答案
这很可能是由于这样的事实:在网页的更新是通过更新面板发生。启动脚本可以使用的问题在这些情况下不烧了,需要包装在:
It's most likely due to the fact that the page update is happening through the update panel. Startup scripts can have issues with not firing in these cases, and need to be wrapped in:
Sys.Application.add_load(function(){ myFunctionCall(); });
这是确保你的启动脚本在Microsoft AJAX库的方式发生在它应该在MS AJAX的往返。
It's the Microsoft AJAX library's way of ensuring your startup script happens when it should during MS AJAX roundtrips.
下面是确保添加到页面中工作的所有客户端脚本,不管它们是否包含在Ajax的UpdatePanel与否的简单方法。我们所有的项目都使用这和它完美的作品:
Here's an easy way to ensure all client scripts added to the page work, regardless of whether they are contained in an ajax updatepanel or not. We use this in all of our projects and it works flawlessly:
<一个href=\"http://stackoverflow.com/questions/1952817/asp-net-javascript-inside-ajax-updatepanel/1953122#1953122\">ASP.Net - AJAX的UpdatePanel Javascript中
ASP.Net - Javascript inside AJAX UpdatePanel
这篇关于从用户控件注入的JavaScript的UpdatePanel内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!