I’m new to working in ASP.NET but I’m developing a custom control that has a multi view control inside it that displays a bunch of different stuff. Some of it is displayed using JQuery UI elements like tabs and accordians which will have quite a bit of customisation.
Since I’m going to have a lot of CSS rules that apply only to the elements inside the custom control (not to the rest of our web site), I’m wondering where to put the CSS style rules.
I’d normally just put a style sheet somewhere in the site root and reference it from there. But as I play around with ASP.NET, I get the feeling that I should be putting all my code (including CSS, JS, etc) inside the custom control itself. This feels more “programmy”, keeping everything together.
Can anyone adivse how I should be doing this? What’s the best practice for web development in ASP.NET?
If the control you are creating is in a separate assembly, you can embed the CSS files inside the assembly to make it reusable and create a direct link to these files from your control, then in your control you will register them to be rendered as
linktags in your pageNote: Remember that you need to mark the CSS file in your assembly as an Embedded Resource
Just select your file | then proeprties and change its Build Action property and set it to: embedded Resource
In the following code:
AjaxEnabled.Web.UIrepresents the namespace of your assemblyDefaultStyle.cssrepresents the embedded CSS file nameThe following code sample shows the steps needed: (in your custom server control)
You need to add an instance of the
ScriptManagercontrol in your page containing your custom controlsAnd in your ASPX page you need to mark the
headersection as a server controlThe following code:
Renders a link directly to the CSS file embedded in the assembly
This condition:
ensures that the CSS is rendered only once in the page, even if you drop more than one instances of your control