0

The Master page got a ScriptManager.

Then i got a Control with a ScriptManagerProxy and an 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.

DynamicControl.ascx:

<script type="text/javascript">
    function doSomething() {
        alert(1);
    }
</script>

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. :S Edit: It is called but not directly when i add the control.

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. And I found out that the javacode is first executed after i close and then open the dialog. 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 ?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try setting the addScriptTags argument to true, and remove the script declaration from your code.

Is that code only supposed to run on every request? If so, why not just add this code to the ASCX:

window.onload = doSomething();

1 Comment

I think this also should work but there is something that is blocking so the code is first executed after some sort of event when i close and open my jQuery Dialog.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.