Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

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

Dec 25, 2011
Introduction: 

Here I will explain how to solve the problem “The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).” when running web application using asp.net. 

Description: 

I created one web application and added some of script files in header section of page like this

<head id="head2" runat="server">
<title>Lightbox Page</title>
<link href="aspdotnetsuresh.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%= ResolveUrl("~/js/LightBox.js") %>"></script>
</head>
After add script file to header section i tried to run the application during that time I got error like The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Server Error in 'ASP.Net' 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.

To solve this problem we have different methods
First Method
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 Method
Replace the code block with <%# instead of <%=
<head id="head2" runat="server">
<title>Lightbox Page</title>
<link href="aspdotnetsuresh.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%# ResolveUrl("~/js/LightBox.js") %>"></script>
</head>
After replace code block with <%# instead of <%= add following code in page load

protected void Page_Load(object sender, EventArgs e)
{
Page.Header.DataBind();    
}
After add code run your application it will work for you.

Happy Coding………

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

50 comments :

sami said...

thanks it works....

Unknown said...

Another solution of this is to put your script inside body rather than putting it inside head section.

Hash Moody said...

It worked thanks man.

Anonymous said...

what a simple but great solutions? thanks man...

Anonymous said...

It worked thanks

Pranav Patel said...
This comment has been removed by the author.
Pranav Patel said...

Gr8. Good article

Shekhawat Sandeep said...

Thanks Its Working, great solution, Please can you explain why <%= is not working in this case?

I am not getting value of property its just remove error.

Anonymous said...

thanks it works....

Anonymous said...

nice solutions...........thanks

Anirban Dutta said...

Your posts are very helpful. Keep us posted with your latest updates.

Anonymous said...

Really thankx , it worked :))

Unknown said...

More than the solution to this problem i want to know exactly why this problem happens!!!!!!!

Anonymous said...

Thanks... It works

poonamtemgire said...

thanks.....

Anonymous said...

Very simple solution...

Anonymous said...

Thanks.. It worked!

Anonymous said...

Thanks A Lot.
It worked for me.

Anonymous said...

Work for me! Thanks man, you're rock!

Anonymous said...

Hi Friend,
Thanks It works.

Anonymous said...

good work

Unknown said...

Thank you very much It worked for me. Can you please explain the reason why this error come?

Anonymous said...

Thanks a lot it works for me......

Anonymous said...

great... was looking for that for 2 days

Unknown said...

Great Thanks a lot dear It is working .

Anonymous said...

thanks lot Mr Suresh

masthan said...

thanks suresh....

kavithakesavan said...

Thanks Suresh.....Really Great Man!!!

Anonymous said...

not working

Unknown said...

thanks it works...

Anonymous said...

Hey thank u it works....

Arvin said...

Great! It working awesome!

Unknown said...

not work for me

Anonymous said...

thank you.it is working

Anonymous said...

Can you please explain the reason why this error come?

Anonymous said...

Doesn't work for me. all code blocks are in the body but still doesn't work.

Unknown said...

thanks it is working fine

Anonymous said...

Thanx Alot

Unknown said...

Suresh Sir i wnat the help form You. i am using the devexpress i wnat to print the data of devexpresspress grid view how can i do this please help me please send the code to my gmail. ID: mohamedasiff28@gmail.com please..

Anonymous said...

thanks dear.

Unknown said...

Thank you very much. Can you please explain the reason why this error come?

Anonymous said...

thank u so much this solution solved my problem

Unknown said...

Thank you very much , its workign for me.

Anonymous said...

Thanks

Jeff Berda said...

I had to remove a <% Response.Expires = -1 %> from the HEAD and it worked

Unknown said...

Thanks Its Works For Me.
Munaf Khan

Unknown said...

I have use gridview in side update panel . how to freeze gridview header

Anonymous said...

I am not getting value of property its just remove error.
input type ="hidden" id ="lblcom" value='<%=Session["comm"]%>'

I am not getting session value if I keep # in place of =

Anonymous said...

very very thanks Mr.Suresh. It helped me a lot. once again thank you so much.

Anonymous said...

no help

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.