哪种以编程方式引用 CSS class 的方式最可靠?

Which way of programmatically referencing a CSS class is most reliable?

在我的 Sharepoint 项目中,我在重写的 CreateChildControls() 方法中动态创建控件。我不知道引用 .css 文件的两种方式中哪一种是首选:

protected override void CreateChildControls()
{
    base.CreateChildControls();

    // Way 1
    // from http://sharepoint.stackexchange.com/questions/18652/programmatically-add-js-css-to-pages
    this.Controls.Add(new CssRegistration()
    {
        Name = "/_layouts/ucsc_web_forms.css"
    });

    // Way 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;
}

...但至少,其中一个应该可以解决问题。一种方法优于另一种方法吗?

更新

根据经验观察(经验反击),第二个 "way" 破坏了 WebPart,至少是我的。第一个:

this.Controls.Add(new CssRegistration()
{
    Name = "/_layouts/ucsc_web_forms.css"
});

...至少 "does no harm"

这对我有用(假设您的项目有一个名为 "ucsc_web_forms.css" 的 CSS 文件,其中 类 名为 "finaff-webform-field-label" 和 "finaff-webform-field-input"):

在 "base.CreateChildControls();" 之后将以下内容添加到 CreateChildControls():

this.Controls.Add(new CssRegistration()
{
    Name = "/_layouts/ucsc_web_forms.css"
});

...然后,像这样创建动态控件就可以了:

LiteralControl reqDateStr = new LiteralControl("<span class=\"finaff-webform-field-label\">Requester Date:</span>");
. . .
boxPayeeName = new TextBox();
boxPayeeName.CssClass = "finaff-webform-field-input";