Removing ASP.NET Calendar Control's Default Style

This problem has been bugging me ever since we hit it the other day at work. It occurred when Al was trying to style a .NET Calendar using an external CSS file. In the properties of the Calendar you can specify the css class using the CssClass fields, these render correctly. However, there are a few properties such as DayStyle which have a default colour that renders in the ‘Style’ tag of the link. If the ForeColor is specified it does render that colour, if you don’t specify, it renders ‘black’. This effectively makes the CssClass field useless for setting the link colour.

After a quick Google, turns out this issue has been discovered some time ago, there is a custom Calendar control on CodeProject that attempts to solve the problem. Probably the best solution out there but just seems like a lot of effort to stop a default ‘Style’ tag from rendering.

So here is another alternative, which I’ll probably put into the ‘dodgy hack’ category. But it does to the job. Simply override the page/usercontrol/custom control’s Render() method, and string.Replace() it. Here's an example:

public class CalendarNoDefault : System.Web.UI.WebControls.Calendar
{
public CalendarNoDefault()
{
}

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
//open a temp stream to save rendered calendar html into
System.IO.MemoryStream memstr = new System.IO.MemoryStream();
System.IO.StreamWriter stream = new System.IO.StreamWriter(memstr);
HtmlTextWriter wt = new HtmlTextWriter(stream);

base.Render(wt);

wt.Flush();
memstr.Seek(0, System.IO.SeekOrigin.Begin);

//read back the html from memory
System.IO.StreamReader rd = new System.IO.StreamReader(memstr);
char[] bytes = new char[memstr.Length];
rd.ReadBlock(bytes,0,(int)memstr.Length);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.EnsureCapacity((int)memstr.Length);
sb.Append(bytes);

//remove the 'default' colour style,
//If DayStyle.ForeColor is set it renders that colour,
//if it is not set it
//renders "color:Black" in the style="" tag by default.
//Still looking for a better way to remove this
sb.Replace("color:Black",string.Empty);

writer.WriteLine(sb.ToString());

memstr.Close();
stream.Close();
rd.Close();
wt.Close();
}
}

Print | posted on Saturday, May 13, 2006 2:03 AM

&uot&uot

Comments on this post

# Don't forget !important

Requesting Gravatar...
You could also just add color:red!important; to the external stylesheet. Overrides anything except user specfied overrides.
Left by Jesse Houwing on May 13, 2006 2:45 PM

# No Luck

Requesting Gravatar...
Jesse,

That was one of the first things I tried, since it was one of the simplest options. Unfortunately, it didn't work so the cogs started turning and a short time later Brendan had the above string replace working.

Al.
Left by Alistair on May 13, 2006 2:56 PM

# ie = ignorant

Requesting Gravatar...
Internet explorer ignores the !important tag and given that over 90% of our traffic appears to be from users with internet explorer there was a need for Al and Brendan to locate an alternative.
Left by jacob on May 20, 2006 10:45 AM

# Cool

Requesting Gravatar...
Your code is great !
Thanks for the work
Left by Dom on Aug 17, 2006 12:26 PM

# Thanks

Requesting Gravatar...
Cheers for the fix.

I love the way all the microsoft examples show you how to set the style in the code which is exactly the situation that CSS aims to resolve. Between that and a Browser that doesn't render HTML correctly they've got everyone on the run.

Bring on IE7
Left by Chemlock on Aug 23, 2006 9:35 AM

# Kill the style tags complete...

Requesting Gravatar...
with regular expressions:

Dim rx As New Regex("style=\""\S+?\""\w?")
writer.WriteLine(rx.Replace(sb.ToString, ""))


or

Regex rx = new Regex("style=\""\S+?\""\w?");
writer.WriteLine(rx.Replace(sb.ToString, ""));


make sure you import/use System.Text.RegularExpressions as well! :)
Left by Richard on Oct 09, 2006 8:42 PM

Your comment:

 (will show your gravatar)
 
Please add 5 and 1 and type the answer here: