Adding to the BODY element

Just before (or during) the rendering page, I would like to add a piece of code (java script). However, when I try to add a new LiteralControl via the Page.Controls property, I get the error:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

      

How do I get around this problem?

+2


a source to share


2 answers


To avoid this problem, follow these steps:



  • Add the control <asp:Placeholder id="Placeholder1" runat="server" />

    to your ASPX
  • In the code behind, add the control to the Controls collection of this placeholder:

    Placeholder1.Controls.Add (MyControl);

+3


a source


You can just add LiteralControl

to your markup and set its property Text

from code.

EDIT:



Another (and probably more correct) option is to use ScriptManager.RegisterClientScriptBlock

0


a source







All Articles