I have a situation where I need to dynamically load a URL prefix.
I wrote a quick control to handle this and it works for the following instance:
<script type="text/javascript" src='<gbn:AdminPath runat="server" id="Id1" />Rest/Of/Path.js'></script>
But the following case (on the same aspx page) does not work:
<link rel="stylesheet" href='<gbn:AdminPath runat="server" id="Id2" />/css/styles.css'>
This shows up in the browser as:
<link rel="stylesheet" href="<gbn:AdminPath runat="server" id="Id2" />/css/styles.css" />
I’ve tried various things, but I can’t seem to get the tags working. Any suggestions?
Thanks
The problem here is that it treats
<link>elements in the<head>section like they are server controls. It does this, I believe, so that you can use app-relative urls (eg “~/myfolder/file.css”) and have them resolved for you. It does not give this same treatment to<script>tags, though.Since they are treated as server controls, you cannot mix inline script and string literals in a property value or it all gets treated as a literal (as you discovered).
To get around this, you have several options, one of which TheGeekYouNeed outline above.
If you still want to do it inline with a public method, you can, but you have to build the whole property value in your code like the following: