Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 374
Armstrongest Avatar asked Aug 18 '10 18:08

Armstrongest


People also ask

What is the meaning of this code script Runat server </ script?

The runat="server" tag tells the . NET compiler to execute the tag on the server. it can be added to any html tags which make them available on server side code.

How do I use a Runat server?

To make these elements programmable, add a runat="server" attribute to the HTML element. This attribute indicates that the element should be treated as a server control. The id attribute is added to identify the server control. The id reference can be used to manipulate the server control at run time.

What is the use of Runat server in asp net?

The runat="server" tag in ASP.NET allows the ability to convert/treat most any HTML element as a server-side control that you can manipulate via code at generation time. Some controls have explicit implementations, others simply revert to a generic control implementation.

What is script tag in asp net?

Script tags are used to add Javascript (or similar) script to the final HTML which is rendered by the browser, and hence allow client-side scripting. <% ... %> tags are ASP tags that are processed on the server side, and will not be present in the rendered HTML.


4 Answers

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>
like image 133
David Avatar answered Oct 07 '22 18:10

David


You can use the ScriptManager for this:

<asp:ScriptManager ID="ScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Path="~/js/somefile.js" />
        </Scripts>
</asp:ScriptManager>
like image 28
Jemes Avatar answered Oct 07 '22 16:10

Jemes


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;
like image 27
jasin_89 Avatar answered Oct 07 '22 18:10

jasin_89


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.

like image 29
Rup Avatar answered Oct 07 '22 16:10

Rup