简体   繁体   English

如何从dynamicall添加的控件加载javascript函数?

[英]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.

相关问题 如何从Repeater控件中调用Javascript函数 - How to call Javascript function from Repeater control 从动态添加的用户控件中调用函数 - Calling a function from a dynamically added user control 当ascx控件添加到页面两次时调用了错误的javascript函数 - Wrong javascript function called when ascx control added to page twice 如何从Javascript函数之一调用用户控件? - How to call User Control from one of the Javascript function? 如何在WPF中上标和下标字符串值Dynamicall - How to superscript and subscript string value Dynamicall in wpf 如何从添加到另一个控件的子控件访问变量? - How to access a variable from a child control that is added to another control? 如何在动态添加的用户控件中执行javascript - How do I execute javascript in a dynamically-added User Control 如何将数组从wpf控件传递到webbrowser控件中的javascript函数? - How can I pass array from wpf control to javascript function in webbrowser control? 如何使用JavaScript函数从ASP.NET AJAX Control Toolkit设置Slider控件值? - How to set Slider control Value from ASP.NET AJAX Control Toolkit with JavaScript function? 在托管WebBrowser控件的应用程序中,如何在由WebBrowser控件查看的页面中调用JavaScript函数? - How might I call a JavaScript Function in a Page Viewed by a WebBrowser Control from the Application Hosting the WebBrowser Control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM