I just copied/pasted this out of some code I wrote recently. I got the idea from Westwind Technologies in some stuff he was doing, but anyway, without further ado:
private void AddStyle(string name, string style, bool isInline)
{
string styleTag = string.Empty;
if (isInline)
styleTag = string.format("<style type=\"text/css\">\n{0}\n</style>", style);
else
styleTag = string.format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", style);
if (this.Page.Header != null)
this.Page.Header.Controls.Add(new LiteralControl(styleTag));
else
{
// fake it by adding a style tag using the ClientScriptBlock
this.Page.ClientScript.RegisterClientScriptBlock(typeof(SomeType), "_" + name, styleTag, false);
}
}
Sample call: AddStyle("mycustomcssname", customCss, true);