c# - CSS href Path from Hidden Filed Value and Extra String -
<link runat="server" href="css/template.css" rel="stylesheet" />
this external css link. , have hidden filed in page:
<asp:hiddenfield id="hdfextra" runat="server" />
i'm setting value hdfextra
on page load
. need combine css href hidden filed, need this:
href="<% hdfextra.value %>css/template.css"
so have extra/css/template.css
href. not know how works.
one way direct modify href on page_load
first give id
link
<link runat="server" id="csslink" href="css/template.css" rel="stylesheet" />
then on page load modify href
as
csslink.attributes["href"] = rootofcss + "css/template.css";
and rendered on page:
<link id="csslink" href="extracss/template.css" rel="stylesheet" />
you can use single string rootofcss
or use hidden field value ask as
csslink.attributes["href"] = hdfextra.value + "css/template.css";
the hidden field have meaning if change user , use on post back, if take value database, there no reason use hidden field.
alternative literal
you can use 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 public string on class, render on page, 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 inside head
little more tricky because head server side control , can not easy add string on link. other way use literal , direct render there output.
for example code
<link runat="server" id="csslink" href="<%=rootofcss%>css/template.css" rel="stylesheet" />
is render server
<link id="csslink" href="<%=rootofcss%>css/template.css" rel="stylesheet" />
and see server control translate symbol <
, not let run expect , render string, go alternative , direct change href code behind.
the same control if move header , move the runat=server
<link href="<%=rootofcss%>css/template.css" rel="stylesheet" />
is render correctly as
<link href="extracss/template.css" rel="stylesheet" />
Comments
Post a Comment