[英]How to load javascript function from an dynamicall added control?
The Master page got a ScriptManager. 母版页上有一个ScriptManager。
Then i got a Control with a ScriptManagerProxy and an UpdatePanel. 然后我得到了一个带有ScriptManagerProxy和UpdatePanel的控件。
Inside the UpdatePanel there I dynamically add a Control (also containing a ScriptManagerProxy) and from that control I need to run some JavaScript code. 在UpdatePanel内部,我动态地添加了一个控件(也包含ScriptManagerProxy),并且需要从该控件中运行一些JavaScript代码。
DynamicControl.ascx: DynamicControl.ascx:
<script type="text/javascript">
function doSomething() {
alert(1);
}
</script>
DynamicControl.ascx.cs: DynamicControl.ascx.cs:
public void Page_Load(object sender, EventArgs e)
{
...
ScriptManager.RegisterStartupScript(
this.Page, this.GetType(), "scriptID",
"<script type='text/javascript'>doSomething();</script>", false);
My problem is that the function "doSomething()" is never called and i dont know why. 我的问题是函数“ doSomething()”从未调用过,我也不知道为什么。 :S Edit: It is called but not directly when i add the control.
:S编辑:当我添加控件时,它被调用但不是直接调用。
If I do code like this, there will be an alertwindow: 如果我这样做,则将出现一个警报窗口:
"<script type='text/javascript'>alert(1);</script>"
Ok I think i need to add some more information: 好的,我想我需要添加更多信息:
The control is dynamical added inside a jQuery Dialog. 该控件是动态添加在jQuery对话框中的。 And I found out that the javacode is first executed after i close and then open the dialog.
我发现在关闭然后打开对话框之后,首先执行了Javacode。 Some kind of event is triggered so that the code is executed there i think.
我认为某种事件被触发,以便代码在那里执行。 Is it possible to force this event?
是否可以强制此事件? so the scripts are executet directly when the control is added ?
因此,在添加控件时直接执行脚本?
placeHolder.Controls.Add(dynamicReportControl);
This c# code doesn't execute the javascript tag immediately ? 此C#代码不会立即执行javascript标记吗?
Please try something like this 请尝试这样的事情
ScriptManager.RegisterStartupScript(
this.UpdatePanel1, this.UpdatePanel1.GetType(), "scriptID",
"<script type='text/javascript'>doSomething();</script>", false);
Where UpdatePanel1 is your UpdatePanel 其中UpdatePanel1是您的UpdatePanel
Try setting the addScriptTags argument to true, and remove the script declaration from your code. 尝试将addScriptTags参数设置为true,然后从代码中删除脚本声明。
Is that code only supposed to run on every request? 该代码仅应在每个请求上运行吗? If so, why not just add this code to the ASCX:
如果是这样,为什么不将以下代码添加到ASCX:
window.onload = doSomething();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.