Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Master Page and File path issues

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

<script type="text/javascript" src="jquery.js"></script> 

The problem is that the path is always relative to the executing aspx page so this will only work if the "jquery.js" file is located in the same folder. To make it work I have to change the line to:

<script type="text/javascript" src="../../jquery.js"></script> 

This is obviously less than ideal because it will only work for pages that are two levels deep from the root folder. If I try the following, IIS throws an error about an unexpected character.

<script runat="server" type="text/javascript" src="~/jquery.js"></script> 

Any ideas?

EDIT: I forgot to mention as well that the script MUST be in the head tag

The current top answer throws a "ASP.NET Ajax client-side framework failed to load." error when I add it to my master page. Its thrown from javascript and not the .Net compiler. If I move the ScriptManager to the head section where it should be I get a compile error about the ScriptManager needing to be inside a form tag.

The third answer throws a "Illegal characters in path." exception from the compiler

EDIT 2: When I add that line to my head tag I get this error from IIS.

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

SOLVED: I took the edited response from the answer below and put it inside an asp:ContentPlaceHolder element

like image 732
TheDude Avatar asked Mar 30 '09 15:03

TheDude


People also ask

How master page and content pages are connected in asp net?

To add a content page in Visual Web DeveloperSelect the Select master page check box, and then click Add. The Select a Master Page dialog box appears. In the Contents of Folder box, click the master page that you want to associate with the page you are creating, and then click OK.

Can we explain master page in asp net?

ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application.

How can I connect master page in asp net?

Add the master page into our project. Right click Project->Add->New item (shown in the picture), After clicking on new item, Window will open, select Web Form->Web Forms Master Page (shown in the picture), After clicking the add button, master page 'site1.


1 Answers

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> 
like image 149
Cᴏʀʏ Avatar answered Sep 23 '22 01:09

Cᴏʀʏ