I have some user controls that I'm loading in SharePoint and I would prefer to have all those styles contained in an external style sheet. What's the best way to link to an external stylesheet in CSS?
Thanks.
I have some user controls that I'm loading in SharePoint and I would prefer to have all those styles contained in an external style sheet. What's the best way to link to an external stylesheet in CSS?
Thanks.
Can you not add a <link rel...> to the head? If not, can you this.page.header.controls.add?
Thanks,
I was looking at that way too complicated like. Your answer solved my problem and will actually work a lot better than the method that I was going to approach it from.
This code will ensure that you only add 1 stylesheet reference to a page regardless of how many web parts you have on it - same code snippet can be used for javascript.
protected override void OnPreRender(EventArgs e)
{
const string stylesheet = "YourStylesheet.css";
if (!Page.IsClientScriptBlockRegistered(stylesheet))
{
Page.RegisterClientScriptBlock(stylesheet,
string.Format(@"<link href=""{0}/{1}"" rel=""stylesheet""/>",
this.ClassResourcePath, stylesheet));
}
base.OnPreRender(e);
}
If you're using MOSS, you can remove this configuration from your code entirely by using SharePoint's built in alternate style sheet property.
Sadly,this configuration isn't available for WSS sites. But the object model does have it. So you can apply it with code in both WSS and MOSS, either in a web part or via something like PowerShell.
In code, once you have a reference to the the SPWeb object, say in a cleverly named variable called theWeb
, you can just assign the URL of stylesheet with the following code:
theWeb.AlternateCssUrl = "http://server/site/library/stylesheet.css";
theWeb.Update();