locked
RegisterStartupScript Is Placing Script Block at Top of the Form RRS feed

  • Question

  • User1724149869 posted

    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?

    Thanks

    Tuesday, January 7, 2014 11:04 AM

Answers

  • User555306248 posted

    RegisterStartupScript - This method adds JavaScript to the web form right before the ending </form> tag.
    RegisterClientScriptBlock - This method adds JavaScript to the web form, right after the <form runat="server"> declaration.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 8, 2014 11:05 PM
  • User-933407369 posted

    hi madennis,

    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
                Page.Response.Write("<script>alert('Response.Write')</script>");
                
    //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);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 10, 2014 3:46 AM

All replies

  • User-760709272 posted

    How are you viewing the html to know where the script is being placed? And does the updatepanel span the entire form?

    Tuesday, January 7, 2014 11:10 AM
  • User1724149869 posted

    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 as expected.

    Tuesday, January 7, 2014 11:21 AM
  • User555306248 posted

    RegisterStartupScript - This method adds JavaScript to the web form right before the ending </form> tag.
    RegisterClientScriptBlock - This method adds JavaScript to the web form, right after the <form runat="server"> declaration.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 8, 2014 11:05 PM
  • User-933407369 posted

    hi madennis,

    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
                Page.Response.Write("<script>alert('Response.Write')</script>");
                
    //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);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 10, 2014 3:46 AM