asp.net用户控件将不会触发里面的javascript的UpdatePanel [英] asp.net usercontrol won't fire javascript inside updatepanel
问题描述
我已经看到了类似的问题,这和答案,但没有人可以解决这个问题。
I've seen similar issues to this and answers but none seem to fix the issue.
我有一个更新面板内的用户控件。我的内部控制用户I输出的JavaScript。
I have a user control inside an update panel. Inside my user control I output javascript.
触发时的JavaScript将不会闪光。如果我移动JavaScript来父页面的用户控件外/的UpdatePanel然后触发。这是没有道理要做到这一点,因为我不能被复制或者整个的JavaScript(不同的网站),或在每添加引用.js文件使用另一个页面上使用此用户控件没有任何复制code ...页它的使用上(同一地点)。这只是少便携
The javascript will not fire when triggered. If I move the javascript to the parent page outside of the usercontrol/updatepanels then it fires. This doesn't make sense to do this as I can't use this usercontrol on another page without either duplicating code...by either duplicating the entire javascript (different site) or adding references to a .js file in every page it's used on (same site). It's just less portable
我只是想输出控制(更新面板内)的JavaScript。
I merely want to output the javascript with the control (inside the update panel).
在UpdatePanel被提及的我在做什么的准确性。如果我把用户控件的UpdatePanel之外,它甚至没有工作。
The updatepanel is mentioned for accuracy of what I'm doing. It doesn't work even if I place the usercontrol outside of updatepanels.
保持简单(这并不为我工作):
Keeping it simple (This does not work for me):
用户控件:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="_location.ascx.cs" Inherits="_location" %>
<script type="text/javascript">
function test() {
alert('Hello World!');
}
</script>
<a href="javascript:void(0);" onclick="javascript:test();">
Find For Me
</a>
家长:
<uc1:_location runat="server" ID="_location" />
在调试铬告诉我未捕获的ReferenceError:测试没有定义
Debugging in chrome tells me "Uncaught ReferenceError: test is not defined"
如果我直接添加JavaScript来的onclick如下,它的工作原理:
If I add the javascript directly to the onclick as below, it works:
onclick="alert('Hello World!');"
和如上所述,移动功能父页面也有效。
And as stated above, moving the function to the parent page ALSO works.
这是因为如果浏览器忽略来自用户控件输出的脚本。
It's as if the browser ignores the script output from the user control.
任何想法?
推荐答案
在运行的UpdatePanel和UpatePanel就是更新自己的内容,那么对他来说是简单的text / html(而不是code),也没有任何分析器运行脚本,并使其可用于页面。
When you run the UpdatePanel and the UpatePanel is update the content of his, then for him is simple text/html (not code), neither have any parser to run the script and make it available for the page.
所以这个内容,
<script type="text/javascript">
function test() { alert('Hello World!'); }
</script>
<a href="javascript:void(0);" onclick="javascript:test();">
Find For Me
</a>
在更新面板运行并将其设置为网页时,脚本部分不会被解析 - 其简单的text / html的网页
after the update panel runs and set it to the page, the script part is not parsed - its simple text/html for the page.
这部分怎么过运行
<a href="javascript:void(0);" onclick="alert('Hello World!');">Find For Me</a>
因为当你点击它的onclick命令的解析就完成了。
because the parse of the onclick command is done when you click on it.
因此,要解决与你有后续的解决方案。
So to work around with that you have the follow solutions.
- 请你的JavaScript外部文件
- 将您的脚本访问Ajax更新(UpdatePanel中) 的
- 注册与<后面一个code脚本href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerclientscriptblock(v=vs.100).aspx\"相对=nofollow>的RegisterClientScriptBlock 或替代功能。
- Make your javascript external file
- Place you script out of the Ajax Update (UpdatePanel)
- Register the script on code behind with RegisterClientScriptBlock or alternative functions.
这篇关于asp.net用户控件将不会触发里面的javascript的UpdatePanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!