AjaxToolkit for 3.5 not working - asp.net

I am trying to use ajaxtool kit downloaded from here for colorpicker.
When i tries to use this on any of my page, it shows me an error
This page is missing a HtmlHead control which is required for the CSS
stylesheetlink that is being added. Please add <head runat="server" />.
What can be the reason.
I am using ToolScriptManager rather than Script Manager as said in the documentation.
I am using Asp.net 3.5 and using colorPicker control under a content page and adding ToolScript Manager in the same place.
As said by rafel
<head>
<title>l</title>
<link href="<%= ResolveUrl("~/css/style.css") %>" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="<%= ResolveUrl("~/css/chromestyle.css") %>"/>
<script src="<%= ResolveUrl("~/js/JScript.js") %>" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript" src="<%= ResolveUrl("~/js/chrome.js") %>"></script>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
If I adds runat in head i starts getting this error
The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>).

I am getting the same sequence of events that Shantanu was getting. It tells me to add the runat=server to the HEAD when I add the AJAX Control Toolkit ComboBox. So I did that and then it started saying
"The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>)." again just like in Shantanu's case.
If I am reading this correctly, is it saying I cannot use the AJAX Control Toolkit's ComboBox if I have lots of inline code in this aspx file (both in java/inline vb as well as HTML/inline vb)? I mean hey I know our aspx pages are ugly and barely hanging on, but seriously?

You can't use <%= ResolveUrl %> when defining your javascript includes when using the Toolkit. Add the references to the header in your code behind instead.

Ajax control is trying to add its styles to the head section of your page, but cannot do that because it cannot find it. Here's how to fix this:
<head runat="server">

Correct format is : <link rel="Stylesheet" href="~/style.css" type="text/css" />
When using Ajax toolkit, don't use <%...%> in the link to external style sheet. Use above link format.

Related

How to call Page.Databind() without affecting other controls like gridview

I am having a weird problem. My pages were working (too much data centric) before I needed to implement some javascript UI. Since these UI are required on many pages I referenced them on my masterpage like the following :-
<link href="<%# Page.ResolveUrl("~")%>StenliStyle.css" rel="stylesheet" type="text/css" />
<script src="<%# Page.ResolveUrl("~")%>js/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="<%# Page.ResolveUrl("~")%>js/jquery-ui-1.10.3.custom.min.js" type="text/javascript"></script>
the moment I added Page.DataBind(); in load event of master page all gridview also started getting binded.
Since I am binding my grid view dynamically and binding them again during post back slow my pages.
How can I stop this behaviour ? I need to use Page.ResolveUrl due to folder structure.
You don't need to call Page.DataBind() to resolve client url on master page. Try to resolve
links with the following way:
<link href="<%= ResolveClientUrl("~/StenliStyle.css") %>" rel="stylesheet" type="text/css" />
<script src="<%= ResolveClientUrl("~/js/jquery-1.10.2.min.js") %>" type="text/javascript"></script>

How to include Javascript file in Asp.Net page

I want to do some client side validation using javascript in ASP.NET page.
I tried using
<script src="../../../JS/Registration.js" language="javascript" type="text/javascript" />
but its not working.
Please help.
If your page is deeply pathed or might move around and your JS script is at "~/JS/Registration.js" of your web folder, you can try the following:
<script src='<%=ResolveClientUrl("~/JS/Registration.js") %>'
type="text/javascript"></script>
add like
<head runat="server">
<script src="Registration.js" type="text/javascript"></script>
</head>
OR can add in code behind.
Page.ClientScript.RegisterClientScriptInclude("Registration", ResolveUrl("~/js/Registration.js"));
Probably the file is not in the path specified. '../../../' will move 3 step up to the directory in which the page is located and look for the js file in a folder named JS.
Also the language attribute is Deprecated.
See Scripts:
18.2.1 The SCRIPT element
language = cdata [CI]
Deprecated. This attribute specifies
the scripting language of the contents
of this element. Its value is an
identifier for the language, but since
these identifiers are not standard,
this attribute has been deprecated in
favor of type.
Edit
Try changing
<script src="../../../JS/Registration.js" language="javascript" type="text/javascript" />
to
<script src="../../../JS/Registration.js" language="javascript" type="text/javascript"></script>
I assume that you are using MasterPage so within your master page you should have
<head runat="server">
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
And within any of your pages based on that MasterPage add this
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script src="js/yourscript.js" type="text/javascript"></script>
</asp:Content>
ScriptManager control can also be used to reference javascript files. One catch is that the ScriptManager control needs to be place inside the form tag. I myself prefer ScriptManager control and generally place it just above the closing form tag.
<asp:ScriptManager ID="sm" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Scripts/yourscript.min.js" />
</Scripts>
</asp:ScriptManager>
Use Fiddler to see what is happening. Then change the path accordingly. You will probably find you get a 404 error and the path is wrong.

jQuery calls in external js file with MasterPages

I am using ASP.NET 3.5 with MasterPages. My master page has the script references to jquery and jquery UI. My web page that uses the master page has a script reference for a custom javascript file for that page. This javascript file has jquery calls in it (i.e. document.ready --> set up input boxes as calendars).
When I run the website in debug from Visual Studio, the input boxes are not set as calendars. However, if I copy the script from the external file and include it in a script block in the web page, the input box becomes a calendar.
I also have an element in the child page (not sure if that makes a difference). I have referenced the external javascript file in the ScriptManager and outside of the ScriptManager and neither work.
Why does jQuery not work in an external javascript file when the jQuery script reference resides in the master page?
Any help would be appreciated.
Thanks
MASTER PAGE CODE
<head id="Head1" runat="server">
<title>Customer Agreement Lifecycle Management System </title>
<link rel="stylesheet" type="text/css" href="~/calms.css" />
<link href="css/ui-lightness/jquery-ui-1.7.1.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=ResolveUrl("~/js/jquery-1.3.2.min.js") %>"></script>
<script type="text/javascript" src="<%=ResolveUrl("~/js/jquery-ui-1.7.1.custom.min.js") %>"></script>
</head>
CHILD PAGE CODE
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script src="<%=ResolveUrl("~/js/rule.js") %>" type="text/javascript"></script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
I want to thank everyone for their suggestions but I made a "bone-headed" mistake. The tags were mistakenly still in the external js file. Once removed, everything works as expected. I apologize for taking up everyone's time (and somewhat embarrassed).
Thanks.
Is the external script include below the jquery script? Maybe it's the order in which the scripts are being loaded and run...
Are you sure the reference to the jQuery file in the child object appears in the head of the HTML document?
If not, put a ContentPlaceHolder in the tag and put the references you need in each child page.

Why doesn't asp.net css link path work outside of the head tag?

Why doesn't asp.net css link path work outside of the head tag?
I have this code in a master page:
<head runat="server">
<title>Untitled Page</title>
<link href="../CSS/default.css" rel="stylesheet" type="text/css" runat="server" />
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
This seems to resolve the CSS link no mater what folder depth the page is at.
I notice that if you use the css link it only resolves to the correct path if it's in the head (if used in the body it does not work).
I know how to get around it by using ResolveUrl, but I am wondering if this is just how it works or if I'm missing something.
ASP.NET does some magic rebasing of urls in link and script tags when you specify runat="server" on the head element of a master page.
There are some details of this strange behavior here.
Server controls will process relative URLs and will output the appropriate URL to the client. <head runat='server'> is a server control that does this. If you remove the runat='server' attribute, you'll see that this address translation won't happen anymore.

Proper way to use JQuery when using MasterPages in ASP.NET?

I have no issues when doing using JQuery in a aspx page without a masterpage, but when I try to use it in pages that have a masterpage, it doesn't work, so I end up putting the jquery files and other script files in the page instead of the master. Now if I have 10 pages, I am doing this for all 10, which I know is incorrect. In the sample masterpage below, where would I put my script files.
<html>
<head runat="server">
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<asp:ContentPlaceHolder ID="ContentPanel" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
I recently used the fancybox plugin and what I did was instead of putting the jquery script and fancybox scripts in the masterpage because I got frustrated on getting it to work, I just put it in the page where I wanted the script to run, specifically at the bottom, right before the closing asp:Content. Of course, now I have the issue of, if I wanted to use the fancybox plugin in other pages, I would put the jquery script and fancybox script on all 5 pages instead of just the masterpage. When dealing with masterpages, where does everything go using my example above?
You would declare your main jQuery scripts within the master page, as you would normally:
<head runat="server">
<link href="/Content/Interlude.css" rel="Stylesheet" type="text/css" />
<script type="text/javascript" src="/Scripts/jquery-1.3.2.min.js"></script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
And then any page specific JS files could be loaded within the Content controls that reference the Head ContentPlaceholder.
However, a better option would be to look into the ScriptManager and ScriptManagerProxy controls - these can provide you with a lot more control over the way your JS files are served to the client.
So you would place a ScriptManager control in you master page, and add a reference to the jQuery core code in that:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="/Scripts/jquery-1.3.2.min.js" />
</Scripts>
</asp:ScriptManager>
Then, in your page that requires some custom JS files, or a jQuery plugin, you can have:
<asp:Content ID="bodyContent" ContentPlaceholderID="body">
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Scripts>
<asp:ScriptReference Path="/Scripts/jquery.fancybox-1.2.1.pack.js" />
</Scripts>
</asp:ScriptManagerProxy>
The ScriptManager allows you to do things like control where on the page scripts are rendered with LoadScriptsBeforeUI (or better yet, after by setting it to False).
I use this method.
<script type="text/javascript" language="javascript" src='<%=ResolveUrl("~/scripts/jquery.js") %>' ></script>
Just place it above your tag...
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
Alright use a different contentplaceholder for your script. It should look like this
<script type="text/javascript" src="myscript.js" />
<asp:ContentPlaceHolder ID="ScriptContent" runat="server">
</asp:ContentPlaceHolder>
Place this tag at the bottom of your masterpage, close to the </body> tag. This will allow you add scripts on the masterpage and also on your pages as well. Make sure that your scripts are loading in the right order by viewing the page source and ensuring the HTML rendered in the right way. Good luck.
Oh one more thing, make sure jQuery and then FancyBox load up before any other js you may have out there or else it won't work. Javascript loads in the order it was called, jQuery must load first!
This is what will work inside a Master page:
For Script file:
<script type="text/javascript" src="<%= ResolveUrl("~/script/scriptFile.min.js") %>"></script>
For CSS file:
<link rel="stylesheet" href="~/styles/CssFile.min.css" runat="server" />
Additional Notes:
Use the minified versions as could as possible (FileName.min.js) and
(FileName.min.css) to reduce loading time and improve SEO.
Put the CSS before the </head> and the script before the </body> to
enhance performance and improve SEO.
The tile character (~) in the path will resolve automatically to the root of your website.
Do not forget to use runat="server" for the stylesheet only. The script must not have runat="server" because it already uses server operators <%= %>.
You can use the online http://jscompress.com/ to compress your javascript and https://csscompressor.net/ to compress your CSS files.

Resources