views:

50

answers:

2

I'm trying to resolve correct paths to javascript scripts in my head section using:

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

In order to resolve the path I need to call databind using Page.Header.DataBind(); What event should I place the databind call in?

Thanks.

Reference: http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/

When I put it in Page_Load as the article suggests it works (only for firefox), but I wonder if this is the correct place.

When I follow this article IE 8 renders:

<script src="/Scripts/jquery-1.4.2.min.js" type="text/javascript" />

and firefox 3.6 correctly renders:

<script src="../../Scripts/jquery-1.4.2.min.js" type="text/javascript" />

Update:

Fixed browser issues by updating a script reference in a referenced user control to use ResolveUrl. Now browser issues are fixed. Still wondering where to put Databind.

+1  A: 

Change <%# to <%=, at which point you no longer need to call Page.Header.DataBind();, since you are not doing any actual databinding in your expression.

See this question for the differences in the ASP.NET tags.

From the master page I use in all my projects:

<script type="text/javascript" src='<%= ResolveUrl("~/js/jquery-1.4.2.min.js") %>' ></script>
Jason Berkan
When I try doing it this way I get: The Controls Collection Cannot Be Modified Because the Control Contains Code Blocks error.
subt13
What version of Visual Studio? I confess to being a bit confused, as my master page <head> section is setup exactly like the one in the reference you just posted, but I do not get any errors at all (Visual Studio 2008 - web application project - .NET 3.5).
Jason Berkan
VS2010 Ultimate web app project, .NET 3.5
subt13
Some quick searching indicates that the error only occurs when you (or some other control) attempts to dynamically add a control to the header. It appears I have never done that, so I have never been bitten by this particular bug.
Jason Berkan
I use page theming, so I think some magic gets added to the header. I do not have code that adds anything to the header at runtime.
subt13
Theming is likely the culprit then - I don't use it on my projects. Sorry I couldn't be more help.
Jason Berkan
A: 

Fixed by adding ID and runat="server" to link (stylesheet), finding control in header, calling databind on link instead of entire header.

subt13