Archive

Archive for the ‘Error’ Category

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


Introduction:
In this post i am going to reason two things:
(1)why error “The Controls collection cannot be modified because the control    contains code blocks (i.e. <% … %>) ” come.
(2)How to solve same

Cause of error
When you add any AJAX extenders to your Web pages of your web application or try to add something in page header of web pages , it  register  scripts in the head dynamically and if any code blocks(like <% … %>) are present in the MasterPage, an error might occur.

My script in master page

<head id="head2" runat="server">
<title>Lightbox Page</title>
 <script type="text/javascript" src="<%= ResolveUrl("~/js/jquery-1.7.2.js") %>"></script>
 <script type="text/javascript" src="<%= ResolveUrl("~/js/script.js") %>"></script>
</head>

Error screen shot

Many method of solving same are:

First Method

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


<script type="text/javascript" src="<%= ResolveUrl("~/js/jquery-1.7.2.js") %>"></script>
to
<script type="text/javascript" src="<%# ResolveUrl("~/js/jquery-1.7.2.js") %>"></script>

and then add this line in page load of web pages in master pages or child page( in my case it was master page.)
Page.Header.DataBind();
like

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

Second Method
Remove JavaScript from the header section of web page and add it to body of the web page .
Third method
Place all your script inside <asp:placeholder runat=”server”> in header of web page
like

<head runat="server">
 <asp:ContentPlaceHolder ID="ScriptIncludePlaceHolder" runat="server">
    <script type="text/javascript" src="<%= ResolveUrl("~/js/jquery-1.7.2.js") %>"></script>
 </asp:ContentPlaceHolder>
</head>

Doing this makes code block a child of the Placeholder control, instead of being a direct child of the Page.Header control.

I hope this Will help you in solving you problem. If it help you just drop a feedback.And Any suggestion is welcomed.

Categories: ASP.NET, ASP.NET MVC, Error Tags:
%d bloggers like this: