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

I am trying to add css form code. My site uses the main page. I get an error. The collection of controls cannot be modified because the control contains blocks of code (i.e. <% ...%>).

my snipet code

 string CssClass = string.Format("{0}/{1}?$BUILD$", BaseImageUrl, CssFileName); HtmlLink css = new HtmlLink(); css.Href = CssClass; css.Attributes["rel"] = "stylesheet"; css.Attributes["type"] = "text/css"; Header.Controls.Add(css); 

Any suggestions?

+4
source share
2 answers

You cannot modify the Controls collection of the header tag, since the code in the header contains blocks of code. So, you have two options:

  • Remove the blocks of code from the header and put the data in the header from the code.

  • Put the container in the header tag (e.g. PlaceHolder ) and add the link to the container's Controls collection.

+7
source

The error is that the control is not yet displayed, and the Javascript code is executed.

I just moved javascript to the bottom of the page.

-1
source

Source: https://habr.com/ru/post/1309641/


All Articles