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

41 comments :

sami said...

thanks it works....

Amar Mishra 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 :))

Dhiraj Goplani 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

Smita Gupta 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......

Mihir said...

Thanks and saved a lot of time...

Anonymous said...

great... was looking for that for 2 days

Sayyed Arshad 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

SHYAM SHARMA said...

thanks it works...

Anonymous said...

Hey thank u it works....

Aravindan D said...

Great! It working awesome!

chandra putra 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.

Krishan Singh said...

thanks it is working fine

Anonymous said...

Thanx Alot

M. Asiff M. Farook 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.

Give your Valuable Comments

Other Related Posts

© 2010-2012 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.