How can I use runat="server" on a script tag in asp.Net
Asked Answered
P

5

16

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

However, for this project, it doesn't run as root.

I can't put runat="server" on a script tag.

I can put it on a link tag, though.

This must be a common problem with a few simple answers.

Pseudonymous answered 18/8, 2010 at 18:52 Comment(0)
J
20

What I've always done is use a normal script tag and put the src in <% %> tags, as illustrated here:

<script language="javascript" src='<%=ResolveUrl("~/App_Themes/MainTheme/jquery.js")%>' type='text/javascript'></script>
Jacquesjacquet answered 18/8, 2010 at 18:56 Comment(4)
Thanx! Yes, now I remember seeing this before.Pseudonymous
Just an FYI, according this post: #779452 it's better to use # (DataBinding Expression) and Bind from behind.Pseudonymous
@dl-_-lb: Never saw that before, but it makes sense. Thanks!Jacquesjacquet
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.Wolsey
C
14

You can use the ScriptManager for this:

<asp:ScriptManager ID="ScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Path="~/js/somefile.js" />
        </Scripts>
</asp:ScriptManager>
Chorus answered 18/8, 2010 at 19:4 Comment(2)
I have in the past done this, yes... I wonder what the best way is though. Does this add any other overhead?Pseudonymous
ScriptManager (or ToolkitManager) is generally the ideal choice in ASP.NET Webforms.Lesalesak
B
5

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:

    <asp:ContentPlaceHolder runat="server" ID="PrintPreviewBlock" Visible="false">
    <script id="PrintPageCall" type="text/javascript" >
        $(function() {
            window.print();
        });
    </script>
</asp:ContentPlaceHolder>

and from code behind:

PrintPreviewBlock.Visible = true;
Bergama answered 7/11, 2012 at 9:39 Comment(1)
This only works if you're putting it in a .master file; you can't do this in a regular aspx web page.Subroutine
R
3

You can use functions inside the path string, though, e.g.

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

However that's the ASP.NET MVC syntax for local paths - I can't remember the forms version off the top of my head.

Rosannrosanna answered 18/8, 2010 at 18:56 Comment(2)
forms version is the same. MVC syntax is inherited from web forms.Toluene
@Joel I meant the UrlHelper class that I'm using is MVC-only. As the others beat me to, the forms version is ResolveUrl on System.Web.UI.Control.Rosannrosanna
P
1

Taken from dailycoding.com:

<script language="javascript" src="<%=ResolveUrl("~/[PATH]")%>" type="text/javascript"></script> 
Pronunciation answered 18/8, 2010 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.