I don't necessarily need to run it at server, however, I would like to use the ~/js/somefile.js syntax.

Previously, I had just set everything with Absolute paths and set my project to be at the root level. SO, I'd just declare all my stylesheets, background images and javascript files something like /css/somefile.css

@dl-_-lb: Never saw that before, but it makes sense. Thanks!
–
DavidAug 18 '10 at 20:01

4

For some, an additional tip that might help if they get the "Controls collection cannot be modified" exception is to wrap the above method in a <asp:PlaceHolder runat="server"></asp:PlaceHolder> tag.
–
Timothy Lee RussellApr 18 '14 at 5:24

You can get fully what you want by wrapping script tag with asp:ContentPlaceHolder
and the you can access it from code behind, for example set will it be executed or not by setting visible property to true or false.
Here is the example: