Within my Sharepoint project, I am dynamically generating controls in the overridden CreateChildControls() method. I am uncertain which approach is more preferable when it comes to referencing the .css file:
protected override void CreateChildControls()
{
base.CreateChildControls();
// Approach 1
// sourced from http://sharepoint.stackexchange.com/questions/18652/programmatically-add-js-css-to-pages
this.Controls.Add(new CssRegistration()
{
Name = "/_layouts/ucsc_web_forms.css"
});
// Approach 2
HtmlLink cssLink = new HtmlLink();
cssLink.Attributes.Add("type", "text/css");
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("href", "/_layouts/duckbilled_platypi_rus.css");
this.Page.Header.Controls.Add(cssLink);
boxPayeeName = new TextBox();
boxPayeeName.CSSClass = "duck-billed-platypi";
boxPayeeName.Columns = LONG_TEXT_BOX_COL_WIDTH;
}
...but I assume that one of them should suffice. Is there a preferred methodology between the two?
UPDATE
After conducting some hands-on testing (the empirical evidence speaks for itself), it appears that the second approach breaks the WebPart, at least within my setup. The first one:
this.Controls.Add(new CssRegistration()
{
Name = "/_layouts/ucsc_web_forms.css"
});
...at the very least, "does no harm"