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
To solve this problem we have different methods
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. |
|||
|
|||
50 comments :
thanks it works....
Another solution of this is to put your script inside body rather than putting it inside head section.
It worked thanks man.
what a simple but great solutions? thanks man...
It worked thanks
Gr8. Good article
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.
thanks it works....
nice solutions...........thanks
Your posts are very helpful. Keep us posted with your latest updates.
Really thankx , it worked :))
More than the solution to this problem i want to know exactly why this problem happens!!!!!!!
Thanks... It works
thanks.....
Very simple solution...
Thanks.. It worked!
Thanks A Lot.
It worked for me.
Work for me! Thanks man, you're rock!
Hi Friend,
Thanks It works.
good work
Thank you very much It worked for me. Can you please explain the reason why this error come?
Thanks a lot it works for me......
great... was looking for that for 2 days
Great Thanks a lot dear It is working .
thanks lot Mr Suresh
thanks suresh....
Thanks Suresh.....Really Great Man!!!
not working
thanks it works...
Hey thank u it works....
Great! It working awesome!
not work for me
thank you.it is working
Can you please explain the reason why this error come?
Doesn't work for me. all code blocks are in the body but still doesn't work.
thanks it is working fine
Thanx Alot
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..
thanks dear.
Thank you very much. Can you please explain the reason why this error come?
thank u so much this solution solved my problem
Thank you very much , its workign for me.
Thanks
I had to remove a <% Response.Expires = -1 %> from the HEAD and it worked
Thanks Its Works For Me.
Munaf Khan
I have use gridview in side update panel . how to freeze gridview header
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 =
very very thanks Mr.Suresh. It helped me a lot. once again thank you so much.
no help
Note: Only a member of this blog may post a comment.