tags:

views:

1798

answers:

6

The ClientScriptManager.RegisterClientScriptInclude method allows you to register a JavaScript reference with the Page object (checking for duplicates).

Is there an equivalent of this method for CSS references?

Similar questions apply for ClientScriptManager.RegisterClientScriptBlock and ClientScriptManager.RegisterClientScriptResource

+2  A: 

I have used css files as Embedded resources.

Gulzar
Yes, but in that case is there a clean alternative to ClientScriptManager.RegisterClientScriptResource for CSS use?
Shawn Miller
+1  A: 

You can add header links to CSS files in ASP.Net codebehind classes like this:

HtmlLink link = new HtmlLink();
link.Href = "Cases/EditStyles.css";
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
this.Header.Controls.Add(link);

You can iterate through the header controls beforehand to see if it is already there. The example shown is from a Page_Load in one of my projects and is inside a conditional expression that only adds the EditStyles.css if the page is supposed to be in "Edit" mode.

For ClientScriptManager.RegisterClientScriptBlock and ClientScriptManager.RegisterClientScriptResource, they have equivalent functions for checking if they've already been registered (e.g., IsClientScriptrResourceRegistered).

CMPalmer
+3  A: 

Short answer: no. You could certainly roll your own functions (as CMPalmer suggests) to take CSS embedded resources (as Gulzar suggests) and embed them on the page.

As a best-practice matter, though, I'm not sure why you would want to do this. If you're making a reusable control that has a lot of CSS styling, my advice would be to just hard-code the class names into the standards-compliant output of your control, and ship the control accompanied by a suggested stylesheet. This gives your users/customers the option of overriding your suggested styles to fit their needs, and in general allows them to manage their CSS setup as they see fit.

Separating style from markup is a Good Thing - you're already headed down the right path in avoiding the various built-in ASP.NET style attributes, but you should take it all the way and keep the CSS out of your .dll.

Herb Caudill
A: 

One more thought: You might want to consider "namespacing" your class names to avoid collisions with common class names that your consumers might already be using. E.g.

<div class="SmillerControls_Toolbar">
    <a class="SmillerControls_Button" ...>...</a>
    ...
</div>

or you could wrap the whole thing in a single "namespace" class and then write your CSS to that:

 <div class="SmillerControls">
     <div class="Toolbar">
          <a class="Button" ...>...</a>
     </div>
 </div>

your CSS would be like

 div.SmillerControls div.Toolbar
 {
     ...
 }

 div.SmillerControls div.Toolbar a.Button
 {
     ...
 }
Herb Caudill
A: 

What I do is use an

<asp:Literal id="cssliteral" runat="server" /> 

in head and then a StringBuilder on PageLoad that contains the dynamic css script.

StingBuilder str = new StringBuilder();
str.Append("<style type="text/css">");
str.Append(".myclass {background-color:#" + mycolor);
str.Append("</style>");

cssLiteral.Text = str.ToString();
guillem
A: 

Just check for the existance of a registered script, and if you find that it's not there, then you will known this is the first time your control is being created. At which point you can just drop a literal control into your page that points to the css file you want.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    With Page.ClientScript
        If Not .IsClientScriptIncludeRegistered("JQuery") Then 
            .RegisterClientScriptInclude("JQuery", "Scripts/jquery-1.4.2.min.js")
            Dim l As New Literal()
            l.Text = "<link href='Uploadify/uploadify.css' rel='stylesheet' type='text/css' />"
            sender.controls.add(l)
        End If
    End With
End Sub

Hope this helps someone.

mindless