Last post Jan 10, 2014 03:46 AM by Happy Chen - MSFT
Jan 07, 2014 11:04 AM|madennis|LINK
I'm developing a custom server control which uses ScriptManager.RegisterStartupScript to invoke a function when the page is loaded.
All works fine except for the following scenario.
The control is contained within an UpdatePanel. On initial page load the control's Visible property is set false so no rendering of the control occurs. A button is clicked which is a trigger for the UpdatePanel and the async Postback occurs. During the
async Postback, the control's Visible property is set to true and the ResisterStartupScript method is called by the control. On the client I see the function being invoked by the script block emitted by RegisterStartupScript. The problem is the script block
was placed in the Head tag of the Form, not at the end of the Form as I expected; this causes the function to fail because the control has not yet been rendered on the page.
Why would ScriptManager.RegisterStartupScript place the script block in the Head tag of the Form during an async Postback after making the control visible?
Jan 07, 2014 11:10 AM|AidyF|LINK
How are you viewing the html to know where the script is being placed? And does the updatepanel span the entire form?
Jan 07, 2014 11:21 AM|madennis|LINK
I'm using Visual Studio's debugger, breaking in the function called by script block emitted from the control. I look at the outterHTML of the document object and see the script block in the Head tag.
The UpdatePanel does not span the entire page, there are other elements above it and it only contains the custom server control. If the control is visible on the initial page load, all works as expected and the script block is at the bottom of the form
Jan 08, 2014 11:05 PM|chetan.sarode|LINK
Jan 10, 2014 03:46 AM|Happy Chen - MSFT|LINK
here are the most common ways
to add a script to the document of page:
//1. add the script on top and outer of <html> tag
//2. add the script before </form> tag
Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus1", "alert('RegisterStartupScript');", true);
//3. add the script after <form> tag
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ShowStatus1", "alert('RegisterClientScriptBlock')", true);