Is it possible to register 2 stylesheets via RegisterClientScriptBlock in an ASP.NET Custom Control (C#)?
I'm working on an ASP.NET Custom Control that uses a lot of JavaScript and CSS. For maintenance purposes, it is easier to keep the files separate. Before I declare my namespace, I've got the following code:
[assembly: WebResource("MyNamespace.Styles.colorbox.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("MyNamespace.Styles.pagination.css", "text/css")]
The CSS files are obviously in a subfolder called Styles. I'm trying to register them in the OnInit of my page using the following code:
// 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 = "<l开发者_如何学Cink href=\"" + Page.ClientScript.GetWebResourceUrl(typeof(DoctypeSelectorControl), "MyNamespace.Styles.pagination.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
this.Page.ClientScript.RegisterClientScriptBlock(typeof(DoctypeSelectorControl), "cssFile", cssPagination, false);
The first one loads up perfectly, but I don't even see a link for the second (pagination.css). I wasn't sure if this was limited to one file this way or if there's a better way to handle this.
FYI: DoctypeSelectorControl is the name of the class if it wasn't obvious.
Thanks for your help!
I did something similar in a project I worked on where I needed to move my CSS to a shared code library. I was able to inject multiple files, the code below is an example of what I used. Just make sure to specify your CSS files as embedded resources.
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);
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.
精彩评论