
ASP.NET Masterpage, JavaScript, and ResolveUrl
October 6th, 2010
Today was an interesting day for many reasons. The one I’m going to share with you has to do with javascript and the ResolveUrl method in ASP.NET Masterpage. I recently added jQuery to our web project at work. We have a strange setup and using /Script/jQuery.js in the script tags src attribute won’t work. For that reason I decided to use ResolveUrl() which gets the URL of a file you pass it (i.e. ResolveUrl(“~/Script/jQuery.js”) ). This worked great until I was going through the application working on something else and randomly received this error message:
System.Web.HttpException:The Controls collection cannot be modifiedbecause the control contains code blocks (i.e. <% … %>).
It only happened on a couple of pages and the others would load fine which added to the confusion. Honestly, this error made little to no sense when I first saw it, so I did what any resourceful individual would. I googled it. Luckily the first result was the answer to my issue! The fix is to use data binding (<%#) instead of writing it out (<%=). It is strange, but it resolves the problem. This is the wonderful explanation and solution: http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/.
[ Tags ]: asp.net, c#, data binding, javascript, jQuery, master page, masterpage, ResolveUrl, script
[ Category ]: programming



