how do you reference relative CSS files from the link attribute?

  • Thread starter Thread starter Flip
  • Start date Start date
F

Flip

I'm having a problem using the tilde in a relative path reference in my
aspx. Has anyone got any tips'n'tricks?

Right now the resolved html is this.
<link id="cssLink" rel="stylesheet" href="~/Resources/main.css"
type="text/css"></link>

It doesn't want to resolve the ~ in the href. Any ideas?
 
I believe tilde (~) works only for server controls & user controls?

I'm having a problem using the tilde in a relative path reference in my
aspx. Has anyone got any tips'n'tricks?

Right now the resolved html is this.
<link id="cssLink" rel="stylesheet" href="~/Resources/main.css"
type="text/css"></link>

It doesn't want to resolve the ~ in the href. Any ideas?
 
Howdy. Thanks for the quick response.
I believe tilde (~) works only for server controls & user controls?
I thought if I put the runat="server" then that would make it a server
control and it would evaluate it. But does this not work in the <HEAD>
tags?
 
No. AFAIK, it only works for 'real' controls.

Howdy. Thanks for the quick response.
I believe tilde (~) works only for server controls & user controls?
I thought if I put the runat="server" then that would make it a server
control and it would evaluate it. But does this not work in the <HEAD>
tags?
 
No. AFAIK, it only works for 'real' controls.
How do you handle the relative references in your pages for CSSs?
 
You can add a placeholder control to your page in the header, then in
the code behind for the page you can call a routine that generates the
css information, puts it in a literal control, then adds the literal
control to the place holder. When you get to the point where you want
to construct the relative path all you should need to do is run it
through the ResolveUrl method of the page object and it will resolve it
to the proper location. You could create a routine that takes the place
holder and the relative path as parameters then put that somewhere easy
to get to (a base page perhaps) so you don't have to think about it
again. Hope that helps/gives you enough information to move forward with.

Have A Better One!

John M Deal, MCP
Necessity Software
 
Hello Flip,

In your code behind...

string link = String.Format("<link id='cssLink' rel='stylesheet' href='{0}'
type='text/css'></link>", Page.ResolveUrl("~/Resources/main.css"));
Page.RegisterStartupScript("css", link);

Granted, this will not put it in the head section... but that shouldnt be
a problem.
 
while all unix webservers support the "~/", IIS support is spotty.
asp.net support consists of rewriting the url at render time. this is only
for Web.UI.WebControls.

-- bruce (sqlwork.com)



| I'm having a problem using the tilde in a relative path reference in my
| aspx. Has anyone got any tips'n'tricks?
|
| Right now the resolved html is this.
| <link id="cssLink" rel="stylesheet" href="~/Resources/main.css"
| type="text/css"></link>
|
| It doesn't want to resolve the ~ in the href. Any ideas?
|
|
 
Thank you Shiva, John, Matt and Bruce for replying. I'm going to try the
suggestions tonight/tomorrow night. Thanks again! :>
 
while all unix webservers support the "~/", IIS support is spotty.
You bring up a good question. I'm coming from j2ee on both linux and win
servers where I used "/" for path separators. What are we supposed to use
in aspx? I've seen both, but the unix style a bit more, is that what we're
supposed to use?

Thanks.
 
Back
Top