0

I have an ascx page on which I want to add a link to a css file.

For this I use :

<link href="myCSS.css" rel="Stylesheet" type="text/css" />

But it doesn't work.

I also try this on the code behind :

System.Web.UI.HtmlControls.HtmlLink link = new System.Web.UI.HtmlControls.HtmlLink();
link.Href = "myCSS.css";
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
link.Attributes.Add("title", "Default");
this.Page.Header.Controls.Add(link);

And this :

System.Web.UI.HtmlControls.HtmlGenericControl myCss = new System.Web.UI.HtmlControls.HtmlGenericControl();
myCss.TagName = "link";
myCss.Attributes.Add("type", "text/css");
myCss.Attributes.Add("rel", "stylesheet");
myCss.Attributes.Add("href", ResolveUrl("myCSS.css"));
this.Page.Header.Controls.Add(myCss);

I put this ascx file on an aspx file which uses a masterpage, maybe the problem is here ?

EDIT

The problem is the intellisense see the file (because it show my CSS classes in the file), but it doesn't apply them on my ascx file.

5
  • What is the structure of your project? Where are the page and the myCSS.css file located? Commented Aug 6, 2013 at 12:54
  • 1
    Is there actually a css file at that location? Run Fiddler2 then load the page. It will show the request for the css file. If it shows a 404 then thats your problem. Commented Aug 6, 2013 at 12:54
  • Have you tried using a <link runat='server'> tag and setting the properties on that? Commented Aug 6, 2013 at 12:54
  • @Andrei @asawyer The aspx and the css are in the same folder @Tim I tried the runat="server" and it doesn't work too Commented Aug 6, 2013 at 13:01
  • can you screenshot the folder structure? Commented Aug 6, 2013 at 13:03

2 Answers 2

1
 <link href="../Mycss.css" rel="stylesheet" type="text/css" />

Try This

Sign up to request clarification or add additional context in comments.

1 Comment

Nice nice. Hope it helps
0

The code you have the minute would assume the CSS file is located in the same directory as your ascx file, you should resolve the path so it always comes from the root of the site i.e.

<link href='<%=ResolveUrl("~/myCSS.css")%>' rel="Stylesheet" type="text/css" />

You could also try just adding the backslash (if they are in the same directory) i.e.

<link href="/myCSS.css" rel="Stylesheet" type="text/css" />

3 Comments

I thought link tags resolve automatically so there shouldn't be a need to use the ResolveUrl, just the tilda
The CSS file and the aspx are in the same folder, I tried your solution but it doesn't work too...
@Pete only if the control is rendered by the server (i.e. runat="server"), @Shadam are you using the full path to where the CSS file is located though? My answer is just an example of how to use ResolveUrl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.