Separate CSS file for ASP.NET web server control - asp.net

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.

Related

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

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

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.

Which is better in asp.net 2.0?

Can anyone tell me themes are better or CSS style sheets are better in asp.net for design?
Please explain the concept also with an example.
A theme can specify both .skin files and .css files. So there no reason not to use themes.
As for skins versus css: Go for css if its css'able.
You should combine them. Use your css files in the theme folder for your normal styling of all the html elements in your website (include all the generated elements).
In the skin file of a control, you can set the default css class. Other properties like the layout and default behaviour of the elements (sample: calender control) are editable here too.
Skin files are good for all layout specific configuration you can't easily do with css, but with the .net properties of the controls.
Basically themes is built for server controls. You can not use themes with html controls.
The css is used for server controls,html controls and tags. If you are using only server controls then you can use "theme" because you can enable or disable theme on control basis, page basis and whole website basis.
In my opinion CSS is best way to design website. because after rendering theme it shows the css style with controls and tags.
You can also use "Theme" and "CSS" together.
Same query is avilable at my post at following link.
ASP.NET 2.0, AppTheme: How can we utilize AppTheme in best way for my ASP.NET WEBSITE
It make you happy

Does anyone use ASP.net (webforms) to dynamically generate javascript and/or css files?

I prefer the use of external css and javascript files. There are however many cases where the content of a javascript or css file needs to be dynamic. I'll usually just transfer the javascript or css to inline or inpage code in my aspx page and handle the dynamic stuff there.
Does anyone have a better approach? Would there be a way to generate entire js or css files using asp.net's regular templating language?
I'm currently using webforms but I'd be interested in solving this issue in MVC as well.
Thanks
I've used a HTTPHandler to send back dynamic javascript before. But not something that inherits from System.Web.UI.Page.
Using a HTTPHandler and an ASHX or AXD is the "ASP.Net" way to send back resources dynamically.
I have used handlers for dynamic css. Depending on what you need, you can do the same for js files.
I had a css file with placeholders for the pieces that needed to be dynamic like ##bacgroundcolor##, and the handler just replaced as appropriate.
I have also used an approach where I use css classes to mark html elements that need special behaviors. Then the static js, looks for this elements and hook the appropriate handlers. This is something that certainly would be even easier with jquery (I did it with regular js back then :().
I've done this in an aspx page before, but in my opinion the WebForm style doesn't suit itself well to rendering strictly javascript or CSS. Every time I've done it, the page has ended up looking quite a bit like classic ASP.
hopefully the actual JavaScript you are using would stay static and you would just pass parameters to the JavaScript methods.
I have taken JavaScript code that had been in the markup of a page and containing things like <%= control.ClientID %> and replaced it with static JavaScript. I refactored the code into a class, I then refactored these variable parts into class members. The page creates an instance of the class, with things like ClientID set. The functions can then be static.

How to use custom CSS with my Sharepoint WebPart?

'ello!
I'm developing my first WebPart for Sharepoint, and now I'm wondering where/how to include/store my CSS. Where should I put my .css files? How should I include them in my webpart?
This is my Approach
protected override void CreateChildControls()
{
CssRegistration.Register("/_layouts/STYLES/WebPartName/styles.css");
}
This ensures the CSS is registered and included only once and gives the opportunity to modify the CSS without redepolying the whole dll.
You should use Web Part Resources which can be linked or embedded. This article does a good job of explaining how to use them:
Best Practices for Managing Web Part Resources
U can also use:
HtmlLink linkCss = new HtmlLink();
//Defining attributes and values of the link tag
linkCss.Attributes.Add("href", "StyleSheet1.css");
linkCss.Attributes.Add("type", "text/css");
linkCss.Attributes.Add("rel", "Stylesheet");
//Add HtmlLink instance to the header of the current page
Page.Header.Controls.Add(linkCss);
Embedding the css into the webpart is fine if you never ever plan to change the CSS for the webpart.
I would recommend you include the css in either a separate file stored in the style library or change a css file linked in the page layout or master page (such as core.css).
Just define unique classes for your webpart if required and leave the definition of how that renders to each website. This allows different areas of your SharePoint implemenation to show the same webpart in different ways.
The means you will never have to release a dll in order to change a minor look and feel issue.

Resources