ASP.NET Head sections is not getting modified? - asp.net

I have following code structure, where i am trying to change the css file href during runtime. I also have the Update Panel, Script Manager in the page.
It looks like that CSS file is not getting changed/loaded by the browser. I am not able to understand why?
<head>
<link runat="server" id="link1" type="text/css" href="TOBE SET ON RUNTIME.CSS"/>
</head>
//Code to change the stylesheet on page load
link1.Attributes["href"] = GetCSSFileName(this.UserId);
Thanks

Change your head section to have ID and RUNAT="server" attribues
<head id="Head1" runat="server">
<link runat="server" id="link1" type="text/css" href="TOBE SET ON RUNTIME.CSS"/>
</head>
You can set CSS stylesheet from codebehind like this
link1.Attributes.Add("href", "mycustomstyle.css");

Related

Css style stops working on partial postback

in a ASP.net C# application
I have a Checkbox with a custom css.
the Css file is:
<link href="css/default.css" rel="stylesheet" type="text/css" />
the check box
<input id="boxcheck" runat="server" type="checkbox" checked="checked" class="customCheckbox" onclick="ChangeSett();"/>
Everything works correctly.
But when I put it inside an update panel. The style is lost when I do a partial postback.
Thanks for any help
I had similar problem.
I solved it moving css <link .. from the <page> to the <header>.
Since I use a MasterPage and I don't want the link in all pages, I found useful putting a ContentPlaceHolder in the MasterPage's Header
<head id="Head1" runat="server">
...
<asp:ContentPlaceHolder ID="HeaderContentPlaceHolder" runat="server"/>
</head>
and then the link inside the desired page:
<asp:Content ID="Content2" ContentPlaceHolderID="HeaderContentPlaceHolder" Runat="Server">
<link rel="stylesheet" type="text/css" href="CSS/my.css" />
</asp:Content>

Why does my inline asp.net not working within <link href>?

I want to add a version number to my js files.
<link href="css/reset.min.css?v=<%= App.Golbal.VERSION %>" media="all" rel="Stylesheet" type="text/css" />
This renders as
<link href="css/reset.min.css?v=<%= App.Golbal.VERSION %>" media="all" rel="Stylesheet" type="text/css" />
[Standard asp.net 4 web applciation]
Can anybody help?
Put it inside PlaceHolder control because link in the title not included in the form tag so no parsing will occur to it as following
<asp:PlaceHolder runat="server">
<link href="css/reset.min.css?v=<%= App.Golbal.VERSION %>" media="all" rel="Stylesheet" type="text/css" />
</asp:PlaceHolder>
As Dante suggests above, maybe change
<%= App.Golbal.VERSION %>
to
<%=App.Golbal.VERSION%>
or
<%=App.Global.VERSION%>
and try that.
Alternatively, like William suggest, set id and runat=server on the link element and apply the value in the server script/code behind.
<link id="lnkCSS" runat="server" media="all" rel="Stylesheet" type="text/css" />
and the server script/code behind, something like
//might need HtmlLink lnkCSS = FindControls("lnkCSS")`
lnkCSS.href = "css/reset.min.css?`v=" + App.Global.VERSION;
I've had similar issues before the way to get around it is make the link an asp:hyperlink and build the link in the code behind and then assign the link to the NavigateURL of the hyperlink.
Can you try removing the white space in "<%= App.Golbal"?
Btw, Global is mispelled :)

AjaxToolkit for 3.5 not working

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.

How to insert javascript in asp.net master pages [duplicate]

This question already has answers here:
ASP.Net Master Page and File path issues
(10 answers)
Closed 9 years ago.
We're having some trouble with including javascript in masterpages. The "~/" root shortcut doesn't seem to work, and so we're having to manually enter in the path to the javascript from where it will be used, for example: "../../tooltip.js"
However, the problem is that if the nested page is in a different path, this approach does not work as the path stays the same despite the nested page being in a different location - any solutions on how to get the path automatically working like it does for the css files?
Thanks!
The ~/ is a special entity in ASP.NET which represents the "application root". The translation only happens when you pass that URL through an ASP.NET server control, such as <asp:Image runat="server" ImageUrl="~/path..." />. Trying to use it in something that is passed as literal text directly to the client, such as ` will be rendered exactly that in the browser.
There are a few solutions to this:
Put your scripts in a predictable place relative to the domain root, such as domain.com/scripts/script.js, and you can refer to it as /scripts/script.js. The preceding / tells the client (browser) it is an absolute path from the domain root.
Use Page.ResolveClientUrl to render the correct path (<%=Page.ResolveClientUrl("~/script./js")%>)
Create your own server control which handles the ~/ resolution.
Embed the script as an assembly resource.
Cory Larson recommends:
<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/tooltip.js") %>"></script>
Try
<script type="text/javascript" src=<%=Request.ApplicationPath+"/Scripts/Script_Name"%>></script>
I've been using url rewriting and "~" occasionally chokes on special characters in the url even when they are encoded.
If the script tag is in the HEAD element just use a path that is relative to the master page and asp.net will automatically fix the reference for you. Just make sure that the HEAD element has the runat=”server” attribute.
This also works really well for CSS files. It's the only way I've been able to get them to resolve in the VS.NET designer.
Here is a sample from my project:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../styles/reset.css" />
<link rel="stylesheet" type="text/css" href="../styles/960.css" />
<link rel="stylesheet" type="text/css" href="../styles/default.css" />
<link rel="stylesheet" type="text/css" href="../styles/buttons.css" />
<script type="text/javascript" src="../jQuery/jquery-1.3.2.js"></script>
</head>
Linked style sheets can be put within the header (with runat="server") and use a ~ to resolve to the root. However, Javascript files cannot be referenced this way. One additional way to add scripts is with a script manager in the body.
<html>
<head runat="server">
<title>title</title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery-1.4.2.js" />
<asp:ScriptReference Path="~/Scripts/jquery-ui-1.8.custom.min.js" />
</Scripts>
</asp:ScriptManager>
</body>
</html>
As answered # [Include Javascript adn CSS][1]
http://www.codeproject.com/Articles/404942/Include-JavaScript-and-CSS-on-your-MasterPage
For CSS files:
<link href="<%# ResolveUrl("~/") %>css/custom-theme/jquery-ui-1.8.21.custom.css" rel="stylesheet" type="text/css" />
For JavaScripts:
<script src="<%# ResolveUrl("~/") %>Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
Can you explain what you mean by the "~/" isn't working? It should be exactly what you're looking for. You might check that your head tag has the runat="server" attribute, since that might prevent the "~/" from working.

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.

Resources