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

后端 未结 5 1074
生来不讨喜
生来不讨喜 2021-02-19 04:00

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

相关标签:
5条回答
  • 2021-02-19 04:12

    You can use the ScriptManager for this:

    <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Scripts>
                <asp:ScriptReference Path="~/js/somefile.js" />
            </Scripts>
    </asp:ScriptManager>
    
    0 讨论(0)
  • 2021-02-19 04:13

    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;
    
    0 讨论(0)
  • 2021-02-19 04:16

    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>
    
    0 讨论(0)
  • 2021-02-19 04:23

    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.

    0 讨论(0)
  • 2021-02-19 04:31

    Taken from dailycoding.com:

    <script language="javascript" src="<%=ResolveUrl("~/[PATH]")%>" type="text/javascript"></script> 
    
    0 讨论(0)
提交回复
热议问题