To add an external css file from the code behind

2020-06-08 06:51发布

问题:

I've a CSS file, say SomeStyle.css. Is it possible for me to apply this style sheet document to an aspx page from its code behind?

回答1:

You can add literal controls to your header control:

Page.Header.Controls.Add(
    new System.Web.UI.LiteralControl("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + ResolveUrl("~/Styles/SomeStyle.css") + "\" />"));