Thursday, June 19, 2008

Javascript include redux

Today I had little accident with my ASP.NET project.
I have a master page which includes some javascript files, and several pages derived from this master page. Today, to better organize my code, I created a sub-folder in which I moved some of these pages. After doing that, these pages were no more able to include my javascript files.
After some "googling".
The include line format was this:

src="Scripts/FmMaskStorico.js"

I tried adding the tilde character:

src="~/Scripts/FmMaskStorico.js"

But with no success. It seems that ASP.NET interprets the tilde character only for server controls, while leaving to the browser the handling of script includes like this.
Another option I found was using the syntax :
src="<%=Request.ApplicationPath%>/Scripts/FmMaskStorico.js"
Unfortunately I can't use this option since I create all controls dynamically and it seems that this is not compatible with the <% %> syntax.
Well at the end I resolved deleting the static includes in the master page aspx, substituting them with some
calls to Page.ClientScript.RegisterScriptInclude.
This way I got it.
Phew .....


No comments: