0

Got stuck on this! Debugging after adding a Rating Control to my aspx.page, I receive the Error:

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

I know that the header <%= System.Web.Optimization.Scripts are the cause.

To recap; everything compiles until adding Rating Control. If I set <%= to <%# + Page.Header.DataBind(); page compiles but obviously not firing scripts.

So need some help with this! Thanks

[HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
   System.Web.UI.ControlCollection.Add(Control child) +9855103
   AjaxControlToolkit.ToolkitResourceManager.RegisterCssReferences(Control control) +829
   AjaxControlToolkit.ExtenderControlBase.OnLoad(EventArgs e) +81
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Control.LoadRecursive() +145
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772


<head runat="server">
<title>bla</title>

<%= System.Web.Optimization.Styles.Render("~/css") %>
<%= System.Web.Optimization.Scripts.Render("~/bundles/jquery") %>
<%= System.Web.Optimization.Scripts.Render("~/bundles/stack") %>    

    $(document).ready(function () {


    });

</script>

Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
tuvboy
  • 71
  • 1
  • 11
  • see http://stackoverflow.com/questions/778952/the-controls-collection-cannot-be-modified-because-the-control-contains-code-bl – A_Sk Sep 23 '15 at 12:32

2 Answers2

1

This example adds resources to the <head> but also works with any control for which Controls.Add() works

For CSS:

System.Web.UI.WebControls.Literal lit = new System.Web.UI.WebControls.Literal();
lit.Text = System.Web.Optimization.Styles.Render("~/bundles/my_css").ToHtmlString();
Header.Controls.Add(lit);

For JS:

System.Web.UI.WebControls.Literal lit = new System.Web.UI.WebControls.Literal();
lit.Text = System.Web.Optimization.Scripts.Render("~/bundles/my_js").ToHtmlString();
Header.Controls.Add(lit);

Try adding from codebehind

Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
1

I changed Page.Header.DataBind(); in my code behind to this.Header.DataBind(); and it works fine! thnx to contributors

tuvboy
  • 71
  • 1
  • 11