Add CSS File in DLL (Class Library)

2019-09-09 04:00发布

问题:

I have a Class Library project named "AddJsFunction" which contains a class where I am including all related JS, CSS and Image files. Below is the IncludeJsFile method inside the above mentioned class where I am adding all the Javascript files which works as expected.

public void IncludeJsFile(Page pg)
    {
        pg.ClientScript.RegisterClientScriptInclude(this.GetType(), "Test2", pg.ClientScript.GetWebResourceUrl(this.GetType(), "AddJsFunction.queue.js"));
        string csslink2 = "<script type='text/javascript' language='javascript' src='" + pg.ClientScript.GetWebResourceUrl(this.GetType(), "AddJsFunction.queue.js") + "' />";
        LiteralControl include2 = new LiteralControl(csslink2);
        pg.Header.Controls.Add(include2);
    }

Same way I want to embed the CSS files which i tried to include like shown above but that is not working and I need some help to do the same.

Thanks, VY.

回答1:

From Is there a ClientScriptManager.RegisterClientScriptInclude equivalent for CSS:

HtmlLink link = new HtmlLink();
link.Href = "Cases/EditStyles.css";
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
//check if it exsists on the page header control first
foreach (Control control in pg.Header.Controls)
{
    if ((control is HtmlLink) &&
        (control as HtmlLink).Href == href)
    {
        return true;
    }
}
pg.Header.Controls.Add(link);