views:

77

answers:

1

Hi,

I was wondering if its possible to have an ASP.NET AJAX custom usercontrol 'register' its use of CSS files like it does 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?

+1  A: 

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());
Gabriel McAdams
Thats what i am trying to do. I have a css file that needs to ship with the control, just like the JS file. I dont want to manually write out the name of the css file, as its embedded along with the JS file in the assembly. In addition to that, if i were to say have the control on a page 50 times (like a text box say), it would try to re-reference the css page 50 times?
mrwayne
If this is what you want to do, then see my modified answer with a code sample.
Gabriel McAdams
I'm pretty sure thats what i'm looking to do. Thanks!
mrwayne