Can anyone tell me why this works:
<script type="text/javascript" src="/js/jqFuncs.js?v=<%=jqFuncsScriptlastWriteTime %>" />
But this doesn't
<link type="text/css" rel="stylesheet" href="/css/site.css?v=<%=sitecsslastWriteTime %>" />
My code behind has:
public string jqFuncsScriptlastWriteTime = System.IO.File.GetLastWriteTime(#"c:/web/cs3/js/jqFuncs.js").ToString("yyMMdd");
public string sitecsslastWriteTime = System.IO.File.GetLastWriteTime(#"c:/web/cs3/css/site.css").ToString("yyMMdd");
The rendered HTML looks like this:
<script type="text/javascript" src="/js/jqFuncs.js?v=131126" ></script>
<link type="text/css" rel="stylesheet" href="/css/site.css?v=<%=sitecsslastWriteTime %>" />
The problem is caused by the way ASP.NET treats LINK tags. Here is another question/answer that provides the solution:
Problem in Expression tag to bind string variable
I would try adding runat="server" first on the link tag. If that does not work, then I would use the other solution that is the accepted answer.
Hie Gordon,
There are some differences between href and src. More details here:
Difference between SRC and HREF
Thanks!
For anyone else searching for the answer i used this:
<%= String.Format("<link type=\"text/css\" rel=\"stylesheet\" href=\"/css/site.css?v={0}\" />", sitecsslastWriteTime) %>
Related
<link runat="server" href="CSS/Template.css" rel="stylesheet" />
this is my external css link. and also I have a hidden filed in page:
<asp:HiddenField ID="hdfExtra" runat="server" />
I'm setting a value for hdfExtra on page load. I need to combine css href with this hidden filed, I need something like this:
href="<% hdfExtra.Value %>CSS/Template.css"
so we have Extra/CSS/Template.css as href. But I do not know how to get it works.
One way is to direct modify the href on Page_Load
First you give an id to your link
<link runat="server" id="csslink" href="CSS/Template.css" rel="stylesheet" />
then on page load you modify the href as
csslink.Attributes["href"] = RootOfCss + "CSS/Template.css";
and we get rendered on page:
<link id="csslink" href="EXTRACSS/Template.css" rel="stylesheet" />
you can use a single string RootOfCss or use the hidden field value that you ask as
csslink.Attributes["href"] = hdfExtra.Value + "CSS/Template.css";
The hidden field have a meaning if you change by the user and you use it on post back, if you just take that value from your database, there is no reason to use a hidden field.
Alternative with Literal
You can use a literal as
<asp:Literal runat="server" ID="cssliteral" EnableViewState="false"></asp:Literal>
and on code behind on page_load
cssliteral.Text = string.Format("<link id=\"csslink\" href=\"{0}/Template.css\" rel=\"stylesheet\" />", RootOfCss);
Using Public String
You can add a public string on your class, then render it on page, for example.
<head runat="server">
<%=FullLink%>
</head>
and on code behind
public partial class PageTest : System.Web.UI.Page
{
public string RootOfCss = string.Empty;
public string FullLink = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
RootOfCss = "EXTRA";
FullLink = string.Format("<link id=\"csslink\" href=\"{0}/Template.css\" rel=\"stylesheet\" />", RootOfCss);
}
Notes
The controls that is inside the head is little more tricky because the head is a server side control and you can not easy add string on the link. Other way is to use a literal and direct render there the output.
For example this code
<link runat="server" id="csslink" href="<%=RootOfCss%>CSS/Template.css" rel="stylesheet" />
is render by the server as
<link id="csslink" href="<%=RootOfCss%>CSS/Template.css" rel="stylesheet" />
and we see that the server control is translate the symbol < and not let it run as we expect and render the string, so we go alternative and direct change the href from code behind.
The same control if we move it from the header and move the runat=server
<link href="<%=RootOfCss%>CSS/Template.css" rel="stylesheet" />
is render correctly as
<link href="EXTRACSS/Template.css" rel="stylesheet" />
I have following code
<link href='<%= Page.ResolveUrl("~/ReportBuilderOutput/Style/jquery-ui.css") %>?Ver=<%= ConfigurationManager.AppSettings["JsCssVersion"].ToString() %>' rel="stylesheet" type="text/css" />
but its rendering like this
http://mysite.com/Reports/%3C%=%20Page.ResolveUrl%28%22~/ReportBuilderOutput/Style/jquery-ui.css%22%29%20%%3E?Ver=%3C%=%20ConfigurationManager.AppSettings[%22JsCssVersion%22].ToString%28%29%20%%3E
I have given page resolve url for two others js, and those are working fine
what I am doing wrong
Thanks
is there runat = "server" in head
Is it possible to do something like this in a head tag, of master page, which has runatserver:
<link rel="Stylesheet" type="text/css" href='<%=Config.ResourcesDomain %>/images/style.css' />
This is not working, as it produces this kind of html:
<link rel="Stylesheet" type="text/css" href="<%=Config.ResourcesDomain %>/images/style.css" />
The reason the output is being rendered like so:
href="<%=Config.ResourcesDomain %>/images/style.css"
Is because ASP.NET is treating the link as an HtmlLink control, and rendering the contents of the href attribute as a literal.
This is a strange quirk of marking the head section as a server control, where certain elements are treated as server controls (even without being marked explicitly with the runat="server" attribute).
Removing the quotations around the href attribute resolves the issue:
href=<%= Config.ResourcesDomain %>/images/style.css
Doing so stops the link element being treated as a server control, thus executing the code block and rendering the correct URL.
However, the above writes the href value out without quotes. Using the following, will add the quotes to the link tag:
href=<%= String.Format("'{0}'", Config.ResourcesDomain) %>/images/style.css
Hope this helps.
Edit
Strangely, if you use double quotes for the href attribute, and include double quotes within the code block this also resolves the issue:
href="<%= "" + Config.ResourcesDomain %>/images/style.css"
However, none of the above are particularly elegant solutions, and setting the URL from the code behind is probably the way to go.
Another solution I've found here: https://stackoverflow.com/a/5727996/368613 --
just place code inside PlaceHolder:
<asp:PlaceHolder runat="server">
... your code with <%= %> tags ...
</asp:PlaceHolder>
mark-up
<head>
<asp:Literal ID="litHead" runat="server" />
</head>
code-behind:
on page_load
litHead.Text = "<link rel='Stylesheet' type='text/css' href='" + Config.ResourcesDomain + "/images/style.css' />";
Update:
do this then
<head runat="server">
<%
Response.Write("<link rel='Stylesheet' type='text/css' href='" + Config.ResourcesDomain + "/images/style.css' />");
%>
<title></title>
</head>
change it to
<link rel="Stylesheet" type="text/css" href='<%Response.Write(Config.ResourcesDomain); %>/images/style.css' />
It should work
Remove the runat="server" attribute on the opening head tag.
This way the asp.net inline code is correctly rendered.
Or do something like this:
<head>
<style type="text/css">
#import "<%= ResolveUrl("~/content/styles.css") %>";
#import "<%= ResolveUrl("~/content/print.css") %>" print;
</style>
</head>
Apparently data binding is required when using the inline tag "<%# %>".
<head id="Head">
//Stuff with inline code
</head>
Code Behind:
protected void Page_Load {
Head.DataBind();
}
Regards
Overlooking something basic here but I am trying to set a variable and have it print in several places on the page.
code behind:
public string myVariable { get {return "40"; } }
page:
<link rel="stylesheet" type="text/css" href="/css/main.css?v=<%=myVariable%>" />
output:
<link rel="stylesheet" type="text/css" href="/css/main.css?v=<%=myVariable %>" />
It seems to have something to do with the quotes as this works when I take it outside of the href. I find that it works fine if I place a string in the code segement.
This works, but isn't what I want:
<link rel="stylesheet" type="text/css" href="/css/main.css?v=<%="40"%>" />
What is the logic behind this behavior and what do I need to do to make it work? I would also settle for a more elegant method of doing this.
You need to single quote the html attribute like so:
<link rel="stylesheet" type="text/css" href='/css/main.css?v=<%=myVariable%>' />
I use this all the time especially within repeaters when I want to create anchor tags
<a href='PageToLinkTo.aspx?id=<%# DataBinder.Eval(Container.DataItem, "Id")%>'>Link Text</a>
This will only work in the body of your aspx page. If you have the link tag in the head section of your aspx page then check out this question for more info: Problem in Expression tag to bind string variable
Why don't you just do like this:
<link rel="stylesheet" type="text/css" <%= ("href='/css/main.css?v=" + myVariable + "'") %> />
I actually had this same issue today and solved it by using a custom code expression builder.
Your code will look something like this:
<link rel="stylesheet" type="text/css" href="/css/main.css?v=<%$ Code:myVariable%>" />
A good tutorial that I used can be found here which I was able to modify to fit my application. This will also work if you need to add code inside of a server side control.
It was really easy to implement.
Here's what I added to my web.config:
<compilation debug="true">
<expressionBuilders>
<add expressionPrefix="Code" type="CodeExpressionBuilder"/>
</expressionBuilders>
</compilation>
And in my App_Code folder I created ExpressionBuilder.vb:
Imports Microsoft.VisualBasic
Imports System.Web.Compilation
Imports System.CodeDom
<ExpressionPrefix("Code")> _
Public Class CodeExpressionBuilder
Inherits ExpressionBuilder
Public Overrides Function GetCodeExpression(ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, ByVal context As ExpressionBuilderContext) As CodeExpression
Return New CodeSnippetExpression(entry.Expression)
End Function
End Class
That was all I did to get it to work.
Try this:
<link rel="stylesheet" type="text/css" href=<%="/css/main.css?v="+myVariable %> />
AFAIK, the whole property must be a code block, like:
href='<%= "css/main.css?v=" + myVariable %>'
In my aspx I have this line:
<link href="<%= Request.ApplicationPath %>/css/ma/screen-ma.css" rel="Stylesheet"type="text/css" media="screen" />
which renders as:
<link href="/XFormPortal/css/ma/screen-ma.css" rel="Stylesheet"type="text/css" media="screen" />
That seems correct, right?
Then i change a single character, lets say add a space between the rel and the type attribute, so now I have the following:
<link href="<%= Request.ApplicationPath %>/css/ma/screen-ma.css" rel="Stylesheet" type="text/css" media="screen" />
Which now rendes as:
<link href="<%= Request.ApplicationPath %>/css/ma/screen-ma.css" rel="Stylesheet" type="text/css" media="screen" />
Okay, what just happend here? The inline code tag is suddenly ignored and written out as text? Because of a single space?
Can anyone explain this?
I would suggest it has something to do with the order in which expressions are parsed by the precompiler... specifically, I suspect that the omission of the space in your first example causes a particular regular expression match to fail, and you essentially escape correct parsing. Use single quotes around your href tag instead.
<link href=<%="\""+Request.ApplicationPath%>/css" rel="Stylesheet" type="text/css" media="screen"/>
Try to remove the runat="server" from the head tag..