Add CSS File in DLL (Class Library)

只谈情不闲聊 提交于 2019-12-11 18:25:55

问题


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);


来源:https://stackoverflow.com/questions/27249580/add-css-file-in-dll-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!