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 '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(, 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();   
    }   
}
Posted by: Brendan Kowitz
Last revised: 21 Sep 2013 12:13PM

Comments

5/14/2006 2:45:13 AM
You could also just add color:red!important; to the external stylesheet. Overrides anything except user specfied overrides.
5/14/2006 2:56:39 AM
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.
5/20/2006 10:45:00 PM
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.
Dom
Dom
8/18/2006 12:26:25 AM
Your code is great !
Thanks for the work
8/23/2006 9:35:31 PM
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
10/10/2006 8:42:56 AM
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! :)

No new comments are allowed on this post.