Is there a way to have a user control ensure its page has a certain css file? - asp.net

(Please note that I'm not talking about this: How to make user controls know about css classes in ASP.NET )
I have some user controls which css styles comes from a small .css file. I would like to know if there is a way for the user control to tell any page it is placed in that the page needs to import this .css file.
What I'm trying to do is avoid forgetting to add a link to the .css file every time I add a usercontrol to a page by just having the control itself say "Hey, page that contains me, I need you to import this .css file if you haven't already".

I would add the css in programatically from the user control itself. This questions gives a good example how
Adding StyleSheets Programmatically in Asp.Net
Therefore the containing pages don't need to know about the css.
Or i guess you could just add it in the usercontrols in some style tags. Depends on your cascade. That one feels a bit dirty to me - i wouldn't sully myself with such unpleasantness (he lies)

Related

Is there a way to associate one stylesheet(s) for one particular template in Meteor?

The way it works now, Meteor automatically finds CSS files in the client directory and renders it without having to call it in the HTML's header tag.
However, how would I be able (if it's even remotely possible) to associate one particular CSS file or files to just one HTML page?
I recently purchased a theme for one section of my website that I didn't want to bother creating myself, since I hate doing UI for my main page myself. But when I place the CSS files of this template with the ones I have for the other template I use, one template's CSS files overwrite the other, so everything ends up looking like a huge jumbled mess.
I hope I'm being clear with my question. Is there a way to get around this?
So, if there's not a way to keep the file from being included in meteor, you could add a class to the body tag of the page you want the styles to appear in, then prepend that class to the beginning of the styles in your new stylesheet, ex: .yourBodyClass#stylehseetID, .yourBodyClass.stylesheetClass
if you put your stylesheet in the public folder, it will be treated as a static file, but then it should go in the header which is rendered by Meteor, and since Meteor is a single page app (SPA), it only has one header for the whole application.
If you need specific styles for a page, you can use some prefix for your css classes. That's probably the easiest.

Django CMS, per page CSS Styles

I often use small, page specific CSS files for a page in Typo3 using css_select. These styles usually apply only to some special element on these pages. Putting these styles in a global file doesn't feel right.
Using css_select I can select a bunch of files that may be included into the page's header, so that it loads it's special styles.
Now I'm looking for a way to do something similar in Django CMS 3. The only built in solution I'd know is to create a new template which seems a bit excessive for a single page where an image needs to be handled a bit differently from all the others, to name just one example.
Is there a way to do this using nothing but django CMS?
If not, is there an app that would do that?
If not, how could an app extend the page admin form in such a way that this function could be added.
You could extend the page.
See http://django-cms.readthedocs.org/en/latest/extending_cms/extending_page_title.html
A good example is https://github.com/nephila/djangocms-page-meta
This the above package allows you to add additional meta tags to page header.

ASP.NET MVC User Control - Where to put the CSS specific to a control?

I am getting ready to code a bunch of ASP.NET MVC User Controls. All those controls are UI and require CSS. What are the best practices for that? Should I have one gigantic CSS file with the code of all controls?
Ideally, I would like each control to have their own CSS file. Is it possible to do that?
Thanks!
I personally would create a "controls.css" or something similar and put all the css associated with your controls in there. When you're ready to deploy, compress and minify all your css into 1 file. I've been playing around with SquishIt lately and really enjoy it.
If you're dead set on keeping the css files separate for each control I would add an extra ContentPlaceHolder to the <head> of your Master Page, right before the closing </head> and call it something like "ExtraScriptsAndCss." That way if your view uses a certain control you can inject the appropriate css or javascript into the head tag.
"User Controls" in MVC are actually "Html Helpers." They're just HTML, so you're free to deploy/distribute them in whatever way makes sense to you. You can put the styles in a single stylesheet, or split them up. As long as the <link rel> tags bring them into the page to which they are added, it will work fine.
I would recommend you add a parameter to your helpers that allows a user to override the default CSS path and filename, in case they want to use their own.
From a pragmatic point of view, I would go for one gigantic css file. It can be minified and cached by the client. This will save you mocking around with trying to put the right CSS into the head of the document.

Separate CSS file for ASP.NET web server control

I have a my own custom web server control.
I created separate CSS file with CSS classes description for this control.
I do not want to add tag to all pages where it is used. So, I believe that there is any way to connect CSS file directly to control. Is there any way to do it?
And what is the best method to add styles to web server custom control?
This question might work for you. It's for a Custom Control, not a Server Control, but it would only change the method in which CSS stylesheet is dynamically added (not Page_Init(), but maybe Render()?).
That seems to be the only way: a must always be added, though automatically, if an external CSS stylesheet has to be used.

ASP.Net Themes, UserControls and CSS

I'm building a Web application with rougly 30 pages, 12 user controls and 3 masterpages.
I've centralized all the CSS in a Theme called Default. The application will expand in the near future.
What is a best practice in separating css files?
Should I:
A - Put everything in one big CSS file separated by comment sections (every page has all styles)
B - Separate the styles per UserControl in a new CSS file (every page has all styles)
C - Same as B, but not storing the UserControl CSS files in the Theme (manual css loading in the ascx).
D - .......?
I recommend separating your styles out into their various types of elements. Have a style sheet for fonts, another for presentation/layout, another for complex div behavior, then beyond that have individual style sheets for exclusion cases.
Then you could have a global style sheet that imports the defaults for all controls, and the the master pages can determine special cases beyond that. I don't recommend separating out by UserControl though. Right now you have a very limited set. I run a site with almost 200 user controls and 200 style sheets would drive me batty.
The advantage of separate css files for your user controls is the ability to use skins. This way you can easily change the styling of your user control by setting Skin="DarkAndCreepy" or Skin="FluffyBunnies".
For user controls which are expected to look the same no matter what, you can stick with the main css file.
I've read in other questions on this site that a single file is the way to go.

Resources