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

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?
 
S

Shiva

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?
 
F

Flip

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?
 
S

Shiva

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?
 
F

Flip

No. AFAIK, it only works for 'real' controls.
How do you handle the relative references in your pages for CSSs?
 
J

John M Deal

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
 
M

Matt Berther

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.
 
B

bruce barker

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?
|
|
 
F

Flip

Thank you Shiva, John, Matt and Bruce for replying. I'm going to try the
suggestions tonight/tomorrow night. Thanks again! :>
 
F

Flip

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top