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

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 :)

Related

favicon not rendering properly from ASP.Net master page

<link href="<%=this.faviconURL %>" type="image/x-icon" rel="icon" />
It will literally print out '
<link href="%3C%25=this.faviconURL%20%25%3E" type="image/x-icon" rel="icon" />
It's as if the runtime is taking the link tag literally and refusing to do any sort of substitution. Can someone explain why this is happening? I would really prefer not using web controls for this.
I think you should add runat to link tag. So It can be processed by Asp.Net.
<link href="<%=this.faviconURL %>" runat="server" type="image/x-icon" rel="icon" />
Apperantly I was wrong see Scriptlet is not working in head tag and you can simple use without runat tag.
<link href="~/server/icofileurl" type="image/x-icon" rel="icon" />
How to add favicon.ico in ASP.NET site

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>

Asp.net delimiter <% replaced with <% in head tag?

Maybe its a stupid question, but i'm having this issue in Visual Studio 2010:
in my Master page i've this code:
<head runat="server">
<title>App Title</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<link href="<%= App.RootPath %>Css/style.css" rel="stylesheet" type="text/css" />
</head>
for some strange reason the <% is changed at runtime with <%
<%= App.RootPath %> works normal if put anywhere outside head tag.
Anyone has never experienced this and resolved?
UPDATE:
If i put off runat="server" in head tag, it works. But i need it.
Edit:
All of these methods work, but the problem is lack of designer support?
The explanation for your trick:
<link <%= "href='" +App.RootPath +"Css/style.css'" %> rel="stylesheet" type="text/css" />
To find the answer generate a compilation exception. Change App.RootPath to App.RootPaths.., then navigate to the source code (it will be shown in the error page). If the compiler matches something like <link href='' rel='' > then it will generate the code to build a corresponding HtmlLink instance. So this is why it parses <%= as a literal string and after that it encodes it.
Your trick cheats the compiler, which is not bad at all.
I believe it does the same thing for meta tags, (HtmlMeta)
For now, i've found this workaroud; still searching for the reason of this behaviour.
<link <%= "href=" +App.RootPath +"Css/style.css" %> rel="stylesheet" type="text/css" />
This should work too.
<link href="<%= App.RootPath + "Css/style.css" %>" rel="stylesheet" type="text/css"/>
I normally use ResolveUrl:
<link href='<%= Page.ResolveUrl("~Css/style.css") %>' rel="stylesheet" type="text/css"/>
**problem**
<link rel="canonical" href="http://www.kayserianadoluhaber.com.tr/haber/<%=kanonikal%>" />
**solved**
<link rel="canonical" href="http://www.kayserianadoluhaber.com.tr/haber/<%=kanonikal+""%>" />

ASP.NET Head sections is not getting modified?

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");

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.

Resources