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

  • Question

  • User-1234271576 posted

    hi am getting this error,why it is coming,i added just below code

    <asp:TextBox ID="TextBox25" runat="server"></asp:TextBox>
    <asp:CalendarExtender ID="TextBox25_CalendarExtender" runat="server"
    Enabled="True" TargetControlID="TextBox25">
    </asp:CalendarExtender>
    <asp:MaskedEditExtender ID="TextBox25_MaskedEditExtender" runat="server"
    CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder=""
    CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder=""
    CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True"
    TargetControlID="TextBox25">
    </asp:MaskedEditExtender>

    but am getting following error

    Server Error in '/' Application.


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

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

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

    Source Error: 

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    Stack Trace: 

    [HttpException (0x80004005): The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).]
       System.Web.UI.ControlCollection.Add(Control child) +8899135
       AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) in f:\TeamCity\buildAgent\work\80acd78aa4c25314\Server\AjaxControlToolkit\ExtenderBase\ScriptObjectBuilder.cs:323
       AjaxControlToolkit.ExtenderControlBase.OnLoad(EventArgs e) in f:\TeamCity\buildAgent\work\80acd78aa4c25314\Server\AjaxControlToolkit\ExtenderBase\ExtenderControlBase.cs:305
       System.Web.UI.Control.LoadRecursive() +74
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Control.LoadRecursive() +146
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
    
    Monday, October 21, 2013 2:59 AM

Answers

  • User-1423995609 posted

    Can you try this approach?

    Instead of <%=...%> use <%#...%>, and call Page.Header.DataBind in Page_Load() event, try and see if that fixes your problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 6:52 AM
  • User555306248 posted

    If there has JavaScript code which contains the <%%> on your page, please try to move it to just before the </body> tag, to see if the issue can be solved.   And there is another thread, which talks about the same issue, I would like to suggest you have a look:   ajaxToolkit:CalendarExtender - Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). http://forums.asp.net/p/1172295/1965060.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 11:42 PM

All replies

  • User-1423995609 posted

    Do you have any javascript code in the <head> section that contains code blocks (<%..%>) ?

    Monday, October 21, 2013 4:37 AM
  • User-1234271576 posted

    yea for scrolling of gridview i used like

    var GridId = "<%=GridView2.ClientID %>";
    var ScrollHeight = 250;
    window.onload = function () {
    var grid = document.getElementById(GridId);
    var gridWidth = grid.offsetWidth;
    var gridHeight = grid.offsetHeight;
    var headerCellWidths = new Array();
    for (var i = 0; i < grid.getElementsByTagName("TH").length; i++) {
    headerCellWidths[i] = grid.getElementsByTagName("TH")[i].offsetWidth;
    }
    grid.parentNode.appendChild(document.createElement("div"));
    var parentDiv = grid.parentNode;

    var table = document.createElement("table");
    for (i = 0; i < grid.attributes.length; i++) {
    if (grid.attributes[i].specified && grid.attributes[i].name != "id") {
    table.setAttribute(grid.attributes[i].name, grid.attributes[i].value);
    }
    }
    table.style.cssText = grid.style.cssText;
    table.style.width = gridWidth + "px";
    table.appendChild(document.createElement("tbody"));
    table.getElementsByTagName("tbody")[0].appendChild(grid.getElementsByTagName("TR")[0]);
    var cells = table.getElementsByTagName("TH");

    var gridRow = grid.getElementsByTagName("TR")[0];
    for (var i = 0; i < cells.length; i++) {
    var width;
    if (headerCellWidths[i] > gridRow.getElementsByTagName("TD")[i].offsetWidth) {
    width = headerCellWidths[i];
    }
    else {
    width = gridRow.getElementsByTagName("TD")[i].offsetWidth;
    }
    cells[i].style.width = parseInt(width - 3) + "px";
    gridRow.getElementsByTagName("TD")[i].style.width = parseInt(width - 3) + "px";
    }
    parentDiv.removeChild(grid);

    var dummyHeader = document.createElement("div");
    dummyHeader.appendChild(table);
    parentDiv.appendChild(dummyHeader);
    var scrollableDiv = document.createElement("div");
    if (parseInt(gridHeight) > ScrollHeight) {
    gridWidth = parseInt(gridWidth) + 17;
    }
    scrollableDiv.style.cssText = "overflow:auto;height:" + ScrollHeight + "px;width:" + gridWidth + "px";
    scrollableDiv.appendChild(grid);
    parentDiv.appendChild(scrollableDiv);
    }

    Monday, October 21, 2013 5:25 AM
  • User-1423995609 posted

    Can you try this approach?

    Instead of <%=...%> use <%#...%>, and call Page.Header.DataBind in Page_Load() event, try and see if that fixes your problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 6:52 AM
  • User555306248 posted

    If there has JavaScript code which contains the <%%> on your page, please try to move it to just before the </body> tag, to see if the issue can be solved.   And there is another thread, which talks about the same issue, I would like to suggest you have a look:   ajaxToolkit:CalendarExtender - Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). http://forums.asp.net/p/1172295/1965060.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 21, 2013 11:42 PM