0

我正在开发一个使用大量 JavaScript 和 CSS 的 ASP.NET 自定义控件。出于维护目的,将文件分开更容易。在我声明我的命名空间之前,我有以下代码:

[assembly: WebResource("MyNamespace.Styles.colorbox.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("MyNamespace.Styles.pagination.css", "text/css")]

CSS 文件显然位于名为 Styles 的子文件夹中。我正在尝试使用以下代码在我的页面的 OnInit 中注册它们:

// Register colorbox css
cssColorbox = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(typeof(DoctypeSelectorControl), "MyNamespace.Styles.colorbox.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
            this.Page.ClientScript.RegisterClientScriptBlock(typeof(DoctypeSelectorControl), "cssFile", cssColorbox, false);

// Register pagination css
cssPagination = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(typeof(DoctypeSelectorControl), "MyNamespace.Styles.pagination.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
            this.Page.ClientScript.RegisterClientScriptBlock(typeof(DoctypeSelectorControl), "cssFile", cssPagination, false);

第一个加载完美,但我什至没有看到第二个(pagination.css)的链接。我不确定这是否仅限于一个文件,或者是否有更好的方法来处理这个问题。

仅供参考:如果不明显,DoctypeSelectorControl 是类的名称。

谢谢你的帮助!

4

2 回答 2

1

I think your actual issue, in the original post, is that you were using the same key. Oddly, though, I would have expected the second overwrite the first.

于 2013-03-29T13:06:03.703 回答
1

我在一个需要将我的 CSS 移动到共享代码库的项目中做了类似的事情。我能够注入多个文件,下面的代码是我使用的示例。只需确保将您的 CSS 文件指定为嵌入式资源。

        string cssColorbox = Page.ClientScript.GetWebResourceUrl(this.GetType(),
            "MyNamespace.Styles.colorbox.css");

        string cssPagination = Page.ClientScript.GetWebResourceUrl(this.GetType(),
          "MyNamespace.Styles.pagination.css");


        HtmlGenericControl colorboxCss = new HtmlGenericControl("link");
        colorboxCss.Attributes.Add("href", cssColorbox);
        colorboxCss.Attributes.Add("type", "text/css");
        colorboxCss.Attributes.Add("rel", "stylesheet");


        HtmlGenericControl paginationCss = new HtmlGenericControl("link");
        paginationCss.Attributes.Add("href", cssPagination);
        paginationCss.Attributes.Add("type", "text/css");
        paginationCss.Attributes.Add("rel", "stylesheet");


        Page.Header.Controls.Add(colorboxCss);
        Page.Header.Controls.Add(paginationCss);
于 2011-04-30T01:44:12.603 回答