Funky
Funky

Reputation: 13608

How do I add a Css file to my Class library?

I have a class library which is being used by another project, it is added in the toolbox, then dragged onto the page. Works great!

Only problem is that I want some styling added.

In my class library I have:

  1. added a css file "PayMyBillClassLibrary.main.css",

  2. added the following to the AssemblyInfo:

    [assembly: WebResource("PayMyBillClassLibrary.main.css", "text/css")]

  3. added the css file in the main class using the following code:

        string cssUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "PayMyBillClassLibrary.main.css");
    
        HtmlLink cssLink = new HtmlLink();
        cssLink.Href = cssUrl;
        cssLink.Attributes.Add("rel", "stylesheet");
        cssLink.Attributes.Add("type", "text/css");
    
        this.Page.Header.Controls.Add(cssLink);  
    

This produces the following in the solution that I mentioned earlier which hold the class library.

<link href="/TestTwo/WebResource.axd?d=DkBjNt27-P0jDJmlJk7Z40MOLq_coWgDQBzgcFJIGLo-9W-RQClfELNEGAApBuu-kkPiYrB_2uU24xTCwXsn5uKPcaWSRY7vHpLnJEGzoCJ9DdIrd-9StVNbLyRKSYvvUA4x2w2&amp;t=634563635276884818" rel="stylesheet" type="text/css" />

If I click on the link in the view source, it just shows a blank page, if I change the CSS reference to murr.css it will say not found. So it seems to be finding the CSS file, but not doing anything with it or picking up the CSSin the file.

Does anyone have any suggestions?

Upvotes: 2

Views: 1624

Answers (1)

James Johnson
James Johnson

Reputation: 46067

You could try putting a literal control in the header, and setting the literal content in code-behind.

<asp:Literal id="cssliteral" runat="server" /> 

Code-behind:

cssLiteral.Text = String.Format("<link href='{0}' rel='stylesheet' type='text/css' />", cssUrl);

Upvotes: 1

Related Questions