How to use jQuery on my page? - asp.net

I'm following this tutorial but it doesn't tell me how to run this jQuery script. Since this script will be run pretty much everywhere, I should attach this script to the Masterpage right, but how?
I guess what I'm asking is, what HTML tag do I need to reference the jQuery script, and where to put the jQuery code.
I have this library already in my project:
Thanks.

jQuery is a client-side scripting tool. ASP .Net is a server-side language.
You are correct, to add a reference to jQuery for all pages it is a good idea to use a master page for this purpose.
In the master page, you simply add the HTML script reference to the master page:
<# MasterPage .... >
<html>
<head>
<script src="../Scripts/jquery-1.4.1.js"></script>
</head>
<body>
<asp:ContentPlaceHolder id="Content" />
</body>
</html>
Add a script element for each of the jQuery scripts. Make sure the jquery-1.4.1.js is the first referenced though.
Also make sure you use the <script></script> instead of <script/> due to some browser issues.
Some newer Visual Studio MVC project files do this script referencing for you (as Lenial mentioned), and this may be easier.

Related

Best way to inject server-side variables/function results into CSS under ASP.NET?

My ASP.NET app has some server-side methods whose results I'd like to inject into my CSS files. For example, rather than hard-code the URL to a logo, I might like to insert a call to MyHelperClass.GetCurrentLogoUrl() in the middle of the CSS file.
If I were writing an ASPX page, I could use code render blocks x (that is, stuff like "<%=MyHelperClass.GetCurrentLogoUrl()%>") in the middle of my HTML markup. It would be nice to do something similar for CSS.
There are some CSS preprocessing frameworks, e.g. dotless, and they seem to have some cool features, but I'm not aware of any of them supporting making C# calls like this.
You could just make an aspx page and link to it when including a stylesheet. You'll miss out on css highlighting though.
<link href="http://domain.com/css.aspx" type="text/css" rel="stylesheet">
This has the potential to generate unnecessary overhead though, especially if most properties you're modifying do not change at runtime. In that case you may want to research TT files which basically generate other files when deployed - in your case, you could have it generate the css with whatever complex logic.
David P's answer to ASP.NET MVC URL auto-resolution in CSS files suggests one approach. If you give your CSS files the .aspx extension and then use a Page directive to tell ASP.NET that it's actually a CSS file, like so
<%# Page Language="C#" ContentType="text/css" %>
then you can write CSS, but also use the normal ASP.NET code render blocks. It feels a little bizarre to name CSS files .aspx, but an initial test suggests this could work.
UPDATE: Props to o.v. for pointing this out even faster than me.
You can inject a classname into the html, something like:
<input type="text" id="myTextBox" cssClass="<%=myClassName%>" />
You can also change the class name in the code behind using the .attributes method.
Add the following into your <head></head> section of your page
<style runat="server" id="cssPhoneResize" type="text/css"></style>
Then from your server-side method:
string logoUrl = "/images/logo2.jpg";
cssPhoneResize.InnerHtml += "#Image{-image: url(" + logoUrl + ");}";
Hope this helps!

registering a javascript file from a master page

Is there any reason why registering a javascript file from the head tags of an ASP.NET master page wouldn't work? For example, I have the following (plus many other) file referece:
<script type="text/javascript" src="/js/jquery/jquery-1.4.2.min.js"></script>
but jquery (and every other JS reference) doesn't work when the page loads.
Any thoughts? Thanks.
The src attribute is relative, try
<script type="text/javascript" src="/js/jquery/jquery-1.4.2.min.js"></script>
and see if that fixes your problem
If that doesn't work, you can try this:
<script type="text/javascript" src="<%# ResolveUrl("~/js/jquery/jquery-1.4.2.min.js") %>"></script>
and in your Page Load handler in the master page add this code:
Page.Header.DataBind();
The masterpage can have script resources just like a normal HTML page; you'll probably want to check the problem with an HTTP debugger like FireBug or Fiddler2, if phsr is correct you'll see the requests failing with an ErrorCode.
Using master pages will not affect the loading of JavaScript files. Your problem is either the location of the file or the script tag is formatted incorrectly.

jQuery Intellisense in VS 2008 not working with ajax ToolkitScriptManager

I've followed all the steps to get intellisense working for jQuery in VS 2008 SP1 (with the vsdoc hotfix). It works when I reference jQuery inside an asp:ScriptManager control like so:
<asp:ScriptManager runat="server">
<scripts>
...
</scripts>
</asp:ScriptManager>
But I'm using ajax ToolkitScriptManager instead and the intellisense doesn't seem to work when using this control. It offers some better features so I'm not willing to live without it.
It looks like the VS team only programmed the jQuery intellisense to look for asp ScriptManager controls and not ToolkitScriptManager. Has anyone found a workaround for this specific problem?
Cheers
Wows, I, had the same problem and used the following method to trick Visual Studio:
<% if(false) { %>
<script src="/scripts/jquery-1.3.2.js" type="text/javascript"></script>
<% } %>
The script tag will never be rendered but VS interprets it and enables intellisense.
If the file is contained in your project with a -vsdoc.js at the end, IntelliSense should work. Rick Strahl has a great post about this at http://www.west-wind.com/Weblog/posts/536756.aspx
A different question: why are you putting the JQuery library in the script manager?
The script manager can do some cool things, like compress the javascript files for you -- but that only works if the JavaScript is in a resource file.
Also, using the ScriptManager adds a bunch of extra JavaScript that will not be downloaded by the client (all of the Microsoft AJAX libraries). Which is fine so long as you are using the Microsoft AJAX Toolkit, but is a lot of extra load if you are not.
EDIT: if you want a better relative position get JQuery from Google Code. You can read about it here: http://code.google.com/apis/ajaxlibs/

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
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>
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.
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>
I do not know whether you guys found the solution to your problem or not. I was facing the same problem and going nuts to figure out why do I get "jQuery is undefined" error on the plugins i use. I tried all the solutions i get from the internet but no luck at all.
But, suddenly something splash on my mind that may be the script files should be in order. So, I moved the jquery referece to first position and everything start working like charm.
Remember guys, if you're using any plugins with jquery, make sure you use the folloing order of setting reference to those fiels.
reference to the jquery library
reference to the other subsequent plug-in libraries and so on...
e.g.:
"script src="js/jquery-1.3.2.min.js" type="text/javascript"...
"script src="js/jqDnR.min.js" type="text/javascript"...
"script src="js/jquery.jqpopup.min.js" type="text/javascript"...
"script src="js/jquery.bgiframe.min.js" type="text/javascript"...
Always make sure you must put the jquery reference to first and then the subsequent libraries.
Hope, this solves your problem especially when you use with MasterPages. Its very strange that it works no matter what order you use when you don't use MasterPages but when you do, then it somehow requres the proper order.
Good luck and happy coding,
Vincent D'Souza
Look at How to Run a Root “/”. This should fix all your issues regarding unresolved .js file paths. You basically reconfigure the VS Dev server to run your application as localhost:port/ as opposed to the regular localhost:port/application name/ making name resolution work the same way as it does on IIS.
For absolute path of the file for any page use it following:
<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
<script type="text/javascript" src="/full/path/to/jquery.js"></script>
If this script tag goes directly to the browser, then you unlikely can substitute your site's root there. At least not on the server. So you can:
Deploy site to the root of domain
name and use absolute paths
(simplest solution).
Insert this
link with server control.
Preprocess resulting HTML before
sending it to the client (with
HttpResponse.Filter).
You can also use <base> HTML tag:
<base href="http://www.domain.com"></base>
and then all the links in header section are relative to base address:
<script type="text/javascript" src="scripts/jquery.js"></script>
It's often useful when you have multiple publishing destinations, like local dev web server, demo server, etc. You just replace that base URL.
<body>
<script language="javascript" src='<%= this.ResolveClientUrl("~/full/path/to/jquery.js") %>' type="text/javascript"></script>
</body>

Usercontrol access Javascript from a Content's Page's Master Page

Hello all I have a problem. I have a masterpage that all of my Content pages inherit from. Within this masterpage I have a script tag pointing to the Javascript file folder ~/Scripts/validation.js
On my content pages I use different usercontrols that require the use of many of the functions within the validation.js file however if I dont put the <script> tag and the Javascript functions within a contentholder on the contentpage the usercontrols do not see these functions and I get errors like OnNameValidation is not defined.
Of course I can copy the Javascript code into all of the pages but that's 30+ pages and a maintenance nightmare if I find a bug in one of the Javascript functions.
So the question (if you haven't already figured out from my long dissertation) is how can I declare the script tag with the path to the validation.js file so that contentpages and their usercontrols etc. can access the functions/code.
What you are trying to do should work, so I suspect that the path to your javascript file is wrong (without seeing your html code I can only assume). Keep in mind that you can only reference the javascript file like this: "~/Scripts/validation.js" if you have the link in a HEAD runat="server" tag. Without the runat="server" it won't find the file. You would have to do something like "../scripts/validation.js"
As a test I would try to call your javascript function in the masterpage, so you can rule out a bad file reference.
I picked up this tip from ScottGu here.
Add this to you user control which enables Intellisense in user controls but always evaluates false:
<% if (false) { %>
<script src="/Scripts/validation.js" type="text/javascript"></script>
<% } %>
I am currently doing this in my site by going to the Source code on the master page and putting the following in the head and outside of the ContentPlaceHolder.
<head>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
<script src="CP.js" type="text/javascript"></script>
</head>
The path you are assigning for your js file is probably not matching in all the pages.
script src="../JavaScript/Scriptaculous/scriptaculous.js"
It should have something like above this if you have separate folder for Scripts, MasterPages, Pages & Controls.

Resources