The Controls Collection Cannot Be Modified Because the Control Contains Code Blocks (I.E. <% ... %>)

The Controls collection cannot be modified because the control contains code blocks

First, start the code block with <%# instead of <%= :

<head id="head1" runat="server">
<title>My Page</title>
<link href="css/common.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%# ResolveUrl("~/javascript/leesUtils.js") %>"></script>
</head>

This changes the code block from a Response.Write code block to a databinding expression.

Since <%# ... %> databinding expressions aren't code blocks, the CLR won't complain. Then in the code for the master page, you'd add the following:

protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
}

asp.net c# javascript The Controls collection cannot be modified because the control contains code blocks (i.e. % ... %)

Here are possible solutions:

First :
Remove JavaScript from the header section of page and add it to body of the page and run your application it will work for you!

Second :

Replace the code block with <%# instead of <%=

After replace code block with <%# instead of <%= add following code in page load

protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();
}

“The Controls collection cannot be modified because the control contains code blocks”

Check if your "PlaceHolder" has a runat=server attribute. That should already help. You can address that control by it's id then instead of using FindControl.

The reason why your Page.Header.Databind() is not working is probably because you don't have a head section with the runat=server like in the example you copied from. They were adding controls in the header there which can be slightly different.

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

It's hard to tell for sure because you haven't included many details, but I think what is going on is that there are <% ... %> code blocks inside your Page.Header (which is referring to <head runat="server"> - possibly in a master page). Therefore, when you try to add an item to the Controls collection of that control, you get the error message in the title of this question.

If I'm right, then the workaround is to wrap a <asp:placeholder runat="server"> tag around the <% ... %> code block. This makes the code block a child of the Placeholder control, instead of being a direct child of the Page.Header control, but it doesn't change the rendered output at all. Now that the code block is not a direct child of Page.Header you can add things to the header's controls collection without error.

Again, there is a code block somewhere or you wouldn't be seeing this error. If it's not in your aspx page, then the first place I would look is the file referenced by the MasterPageFile attribute at the top of your aspx.

Error :The Controls collection cannot be modified because the control contains code blocks (i.e. % … %)

I see that you use in too many points the <% … %> but you also use it inside the UpdatePanel.

When you include the <% … %> then this processing are made during the page render and not on code behind, on the events of the page. Now if the updatepanel for example try to update from code behind and send data, this part of the page is fails because is never renders again. Or if for any reason this part of the page done before the code behind then we also have some issue. This can be done if you call the <% … %> and inside him you make a call to a code behind functions that try to change some other control on page before this call.

All this sound complicate* ? no its not, you just need to have in your mine how the processing on the web page done and follow it with your mine, and see if you make calls that can not be affect the output because what they affect they all ready have been render.

To solve this issues you need to remove the on page render <% … %> inside the update panels Instidead of <% … %> use a Literal control, and render inside the literal control your output on code behind, so there is no issues when the updatepanel for example needs to render his part.

So change this

<%=cMyInput.CliendID%>

to

 <asp:literal run="server" id="txtRenderOnMe" EnableViewState="false" />

and on code behind

protected void Page_Load(object sender, EventArgs e)
{
txtRenderOnMe.Text = cMyInput.CliendID;
}

From your code that you use a lot the <%= ...%> do not go to change all points, only the points that have this issue, probably the one on the UpdatePanel.

*(or I do not describe them good)

The Controls collection cannot be modified error when using asp Menu control

I got this problem a long time back. I moved my javascript from the head tag to a form tag and it fixed the problem. These links helped me a lot :

"The Controls collection cannot be modified because the control contains code blocks"

http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/

The Controls collection cannot be modified because the control contains code blocks (i.e. % ... %). cant get this error out

Calling Page.Header.Databind() does nothing if you don't have databinding expressions (<%# %>) in the header. The <%= ...%> is a code block, not the databinding expression. Just try to change it to <%# ... %> instead.



Related Topics



Leave a reply



Submit