The same goes for attaching custom CSS to controls within SharePoint. One option is to just emit the CSS code (or even to create a link to a CSS style sheet) directly onto the page with the ScriptManager. This would solve the problem of the code being rendered multiple times for the same control over and over again, but it still provides a pretty invasive, obtrusive implementation of the CSS as developers will have <style> tags and CSS code all over their output HTML.
For external CSS files, SharePoint provides a similar set of static methods on the Microsoft.SharePoint.WebControls.CSSRegistration class. The static Register methods found on this class will allow cache-safe links to CSS files. These classes’ static Register implementations correspond with the ScriptLink and CSSLink controls found within the SharePoint MasterPage. These controls effectively loop through all their registrations and emit unique cache-safe <script> and <link> tags for each resource registered—so that the same external resource doesn’t get included multiple times.
Using the ScriptLink and CSSRegistration classes to register external resources allows developers to maintain a cleaner render environment for their SharePoint customizations. It also has the added benefit of providing cache-safe and unique links to these resources at the top of our HTML document. These solutions help developers construct cleaner and more maintainable customizations to their SharePoint environments.
Jonathan Mast is a consultant with SharePoint911.