<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" />
Related
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) %>
Q:
I want to add favicon.ico to my web application .so i add the icon to my solution then, i write the following:
Login page :
<link rel="shortcut icon" href="<%=ResolveUrl("~/favicon.ico")%>"/>
every thing is okay.
Master page of the other pages:
<link rel="shortcut icon" href="<%=ResolveUrl("~/favicon.ico")%>"/>
shows the following error :
The Controls collection cannot be modified because the control
contains code blocks (i.e. <% … %>).
If i use <%# instead and :
protected override void OnLoad (EventArgs e)
{
base.OnLoad (e);
Page.Header.DataBind ();
}
Is this less performance ?and how to fix this problem?
You dont need to use any form of databinding, simply put runat="server" and the runtime will pares the tag as a GenericHtmlControl and the Url attribute will be resolved in the same manner as a normal ASP.Net ServerControl.
<link rel="shortcut icon" runat="server" href="~/favicon.ico" />
// will render as
<link rel="shortcut icon" href="favicon.ico" />
I want to add version number to CSS files for production environment not to force user every time press Ctrl+F5 to download new CSS files after new version is deployed in prod server as the old version is cached in browser.
I'm adding build number as querystring param like this.
link href="Styles/jquery.Autocomplete.css?<%= Globals.BuildNumber %>" rel="stylesheet" type="text/css"
but the output is this
link href="../../Styles/jquery.Autocomplete.css?<%= Globals.BuildNumber %>"
Looks that scriptlet is not working in head tag, as this is fine in body tag.
Remove runat="server" attribute (It escapes the href string) from <head> tag.
<head >
<title></title>
<link rel="Stylesheet" href="Styles/jquery.Autocomplete.css?<%= Globals.BuildNumber %>" .. />
</head>
but this is not a single (and probably not good) solution. (for more info read article). No need to remove runat="server" attribute if you can write this code:
OR use HtmlLink (System.Web.UI.HtmlControls namespace)
myHtmlLink.Href = "Styles/jquery.Autocomplete.css?" + Globals.BuildNumber;
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");
Header.Controls.Add(myHtmlLink);
It's not documented anywhere, but as you've found out it doesn't appear that HtmlHead supports embedded code blocks. Here's one way to get around that. (if it's not obvious, it should go in your code-behind file):
const int BuildNumber = 1;
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
// add style sheet
Header.Controls.Add(new Literal() {
Text = string.Format(
#"<link rel='stylesheet' type='text/css' href='custom.css?{0}' />",
BuildNumber
)
});
}
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 %>'