4
votes

I am trying to add a css form code . My website uses a master page . I am getting the error The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

my code snipet

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?

2

2 Answers

7
votes

You can't modify the Controls collection of the head tag, as the code in the header contains code blocks. So, you have two options:

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

  • Put a container inside the head tag (for example a PlaceHolder) and add the link to the Controls collection of the container.

-1
votes

The error is because the control is not yet rendered, while the Javascript code is executed.

I just moved the javascript to the bottom of the page.