Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Handling styles in your custom control

Note: this entry has moved.

My previous post didn’t stress enough the main point I was trying to make (I should have entitled it differently I believe). I’m posting again, this time hoping to make it clear.

 

I already know you can write all kind of hacks (as Cazzu and Jason have suggested in the comments) to actually make the Calendar output the styles you want. But the intention of my previous post was not exactly to show how to overcome Calendar’s current bugs. Note that I didn’t even mentioned the approach we finally took to replace the background color of each day’s cell, which –btw– didn’t required a single line of code (bets are open…).

 

I was far more interested in showing what the Calendar was doing wrong with the hope that all controls developers out there could learn from this and avoid such a mistake when coding their own controls. So, let’s recap, what was the lesson learned from looking at Calendar’s inner workings?

 

Never, ever, ever, apply any default styles if a CssClass is specified (you know you don’t want to override the page developer settings with your own defaults).

 

(Does the bolded text help now?)

 

Also, the fact that this bug has made its way from v1.0 to Everett and it’s now about to make it to Whidbey troubles me a bit. I know… I know... it’s just the Calendar control (anyone out there using it?) but the fix doesn’t seem to be hard at all so it would be great to see this finally fixed.

 

7 Comments

  • I can see where you're coming from, but I don't see any other reasonable way of getting a default style.



    I'm by no means a CSS expert, but in IE at least you can override the default style using the !important keyword. E.g. you could set Calendar1.TitleStyle.CssClass="CalendarTitle", then include in your CSS file something like:



    table.CalendarTitle td

    {

    background-color:Maroon !important

    ... other styles ...

    }



    I don't think your suggestion of ignoring default styles when a CSS class is specified is a viable solution. The CSS class is not the only selector that may be used in a CSS file, so should not be the only criterion to decide whether to apply a default style.

  • I *might* be using that control if not for the bug you pointed out. I assumed it would be fixed at some point in the last 2 years...

  • Shanon, you CAN use it. See my SOLUTION (NOT hack! see next comment).

  • Cazzu,



    You're creating two new classes just to make the Calendar work properly with a class selector while at the same time you're totally breaking the use of inline styles (ouch!). Why in the world do you need to write these two classes? Simply, because Calendar is buggy, so you're coding around a bug, hacking, winning something over here, losing something over there.



    Now, if you wanna sell me the history that you've created a "new" control with the "feature" of properly rendering with a class selector and with the intention of explicity not supporting inline styles, a sort of "Css Class Only Calendar", thats ok, but I'm not buying it :-)



    -VGA

  • I dig the solution you show here Daniel. I will have to try it out soon. I've wrote off the Calendar as useless almost 2 years ago, why'd you wait so long?

    ;)

  • Daniel,



    In trying to make your *hack* look like a *feature* you've just added lots of new reqs, ie. proyect-wide enforcing the non-use of inline styles, that were not there in my initial post. We may agree in calling it a "solution" as it actually solves the problem of inline styles overriding your css class but I'm still far from calling this a "new" control, sorry! :-)



    -VGA

  • Joe,



    Your suggestion about using the !important syntax was already made in another comment to my original post. Personally I don't like the use of !important.



    I agree class is not the only selector you may use so maybe the logic to select when to use default styles should be more complex: ie. if not selectors (class, ID, whatever) are specified then go with the default styles; but I still do believe that by specifying a class selector I'm giving enough clue to the control to not override it with his default styles.



    -VGA

Comments have been disabled for this content.