ASP.Net Master Page and File path issues

后端 未结 10 1306
余生分开走
余生分开走 2020-11-28 02:33

I\'m trying to add a script reference to jQuery in my master page so that it will work for any page. It currently looks like this



        
相关标签:
10条回答
  • 2020-11-28 02:42
    <body>
    <script language="javascript" src='<%= this.ResolveClientUrl("~/full/path/to/jquery.js") %>' type="text/javascript"></script>
    </body>
    
    0 讨论(0)
  • 2020-11-28 02:45

    You could use a ScriptManager:

    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Path="~/jquery.js" />
        </Scripts>
    </asp:ScriptManager>
    

    EDIT: If you absolutely need this in your <head> section, you could do something like:

    <head>
        <script type="text/javascript" 
            src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
    </head>
    

    EDIT 2: According to the comments, if you are observing that

    The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

    you may need to change the above to use the data-binding syntax:

    <head>
        <script type="text/javascript" 
            src="<%# Page.ResolveClientUrl("~/jquery.js") %>"></script>
    </head>
    
    0 讨论(0)
  • 2020-11-28 02:45
    <script type="text/javascript" src="/full/path/to/jquery.js"></script>
    
    0 讨论(0)
  • 2020-11-28 02:47

    For absolute path of the file for any page use it following:

    <script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script> 
    
    0 讨论(0)
  • 2020-11-28 02:49

    If you're not going to us asp:ScriptManager or absolute paths then you can do it like this:

    <script runat="server" type="text/javascript" 
      src='<%= Page.ResolveUrl("~/jquery.js") %>'></script>
    
    0 讨论(0)
  • 2020-11-28 02:59

    Try <%# instead of <%= in Master page under head section

    <script type="text/javascript" 
            src="<%# ResolveUrl("~/YourScriptFolder/YourJQueryOrJavascript.js") %>">
    </script>
    

    Then in Code Behind of Master page under Page_Load Event

    Page.Header.DataBind();
    

    Now you are good to go with either jQuery and JavaScript as well as CSS just you need to change your path in ResolveUrl which file you want to handle CSS, JavaScript, jQuery.

    0 讨论(0)
提交回复
热议问题