开发者

How to have an asp.net ajax control automatically reference css files

开发者 https://www.devze.com 2022-12-16 08:51 出处:网络
I was wondering if its possible to have an ASP.NET AJAX custom usercontrol \'register\' its use of CSS files like it does开发者_StackOverflow社区 with JS files.?

I was wondering if its possible to have an ASP.NET AJAX custom usercontrol 'register' its use of CSS files like it does开发者_StackOverflow社区 with JS files. ?

Eg: Implementing the IScriptControl interface allows for the GetScriptReferences() to be called by the Script Manager, is there a similar thing for CSS files?


No.

You could write your own methods to use embedded resources and generate html to write them to the page.

As a best-practice approach, though, I wouldn't recommend this. You should have a clear separation of style and markup. If you want to write a custom control that is dependent on a stylesheet, then I suggest that you provide a default css file along with your control.

EDIT (to answer questions in the comments)

You can run this code to add a reference to the CSS file to your page (at runtime). Note that because you're using the RegisterClientScriptBlock method, you can manage insert duplication. Here is an excerpt from the link for the method:

A client script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.

StringBuilder sb = new StringBuilder();

sb.Append(@"<link rel=""stylesheet"" type=""text/css"" href=""");
sb.Append(this.Page.ClientScript.GetWebResourceUrl(this.GetType(), resourceName));
sb.Append(@""" />");

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyCSS", sb.ToString());
0

精彩评论

暂无评论...
验证码 换一张
取 消