Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a ClientScriptManager.RegisterClientScriptInclude equivalent for CSS

Tags:

css

asp.net

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

like image 225
Shawn Miller Avatar asked Sep 10 '08 16:09

Shawn Miller


4 Answers

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.

like image 141
Herb Caudill Avatar answered Nov 05 '22 19:11

Herb Caudill


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).

like image 34
CMPalmer Avatar answered Nov 05 '22 20:11

CMPalmer


I have used css files as Embedded resources.

like image 2
Gulzar Nazim Avatar answered Nov 05 '22 20:11

Gulzar Nazim


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.

like image 2
mindless Avatar answered Nov 05 '22 19:11

mindless