Search code examples
asp.netstylescustom-server-controls

ASP.NET Custom Control Styling


I am in the process of beginning work on several ASP.NET custom controls. I was wondering if I could get some input on your guys/girls thoughts on how you apply styling to your controls.

I would rather push it so CSS, so for the few controls I have done in the past, I have simply stuck a string property which allows you so type in the string which in then slung in a "style" attribute when rendering. I know I could also use the "CSSClass" property and apply the "class" attribute.

I have not done much in the way of creating a "proper" Style property (in which you actually save the style object, and use the designer to specify its values). This to me seems like a lot of work, and TBH, I hate the Style editor UI and would much rather type in the CSS/class name to apply..

What are your thoughts on this?


Note: This is kind of subjective - so to be clear:

The accepted answer will be the one that:

  • Offers the pro's and con's of the various approaches.
  • Opinions are welcome, but a good answer should be constructive.
  • Backs it up with some real-world knowledge/experience.

There is nothing wrong with subjectivity. There is a problem with people being subjective and not thinking, being constructive or actually providing some insight and experience.

>>DO NOT<< tag this as "subjective" - that tag is a waste of time. "subjective" is not a technology or a category that people will look for. Fix the question rather than brush it off.


Solution

  • It would depend on how the custom controls are being used - A commercial, re-distributable control should be compliant with the VS IDE, and behave the way users expect it to when they implement the control.

    On the other hand there is no point in wasting a lot of time to get styling to work if you or your team are the only ones to use the control, so long as it's styling works in a sane way.

    Most of the custom controls I have implemented use a property to define the controls look and feel or just expose the controls' members own CSSClass properties.

    The argument comes down to consistency vs. time - any element should use consistent styling mechanisms, if strapped for time, use a string method if not, implement a more complex / IDE friendly mechanism.