views:

1700

answers:

3

Sometimes when displaying a calendar it is necessary to prevent the weekend days and the weekend names in the day header from showing, is there a way to do this using the ASP.NET Calendar control?

A: 

As far I know you can't, but you can experiment with WeekendDayStyle, for example by setting style with display:none. Alternatively, you can create custom control inherited from Calendar and override ether Render, OnDayRender or something else.

Mike Chaliy
+1  A: 

I believe you can handle the Day Render event and hide the cell or assign CSS properties to make it invisible or grayed out. Below is a simple example, I hope this helps.

protected void Calendar_DayRender(object sender, DayRenderEventArgs e)
{

  e.Cell.Visible = False;
  // or
  // e.Cell.Attributes.Add("class", "Invisible");
  // or
  // e.Cell.Attributes.Add("style", "display: none");
}
James
This just hides the contents of the day field, you would still need to do additional work to be able to remove the day heading.
Rob
+5  A: 

As the control is provided, there is no way to do this without overriding the control. One way of doing this is to is to override the OnDayRender and Render methods to remove the information from the output prior to sending it back to the client.

The following is a screen shot of what the control looks like when rendered:

Example of weekday calendar

The following is a basic control override that demonstrates removing the weekend day columns from the control.

/*------------------------------------------------------------------------------
 * Author - Rob (http://stackoverflow.com/users/1185/rob)
 * -----------------------------------------------------------------------------
 * Notes
 * - This might not be the best way of doing things, so you should test it
 *   before using it in production code.
 * - This control was inspired by Mike Ellison's article on The Code Project
 *   found here: http://www.codeproject.com/aspnet/MellDataCalendar.asp
 * ---------------------------------------------------------------------------*/
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Xml;

namespace DataControls
{
    /// <summary>
    /// Example of a ASP.NET Calendar control that has been overriden to force
    /// the weekend columns to be hidden on demand.
    /// </summary>
    public class DataCalendar : Calendar
    {
        private bool _hideWeekend;
        private int _saturday;
        private int _sunday;

        /// <summary>Constructor</summary>
        public DataCalendar()
            : base()
        {
            // Default to showing the weekend
            this._hideWeekend = false;
            // Set the default values for Saturday and Sunday
            this.Saturday = 6;
            this.Sunday = 0;
        }

        /// <summary>
        /// Indicate if the weekend days should be shown or not, set to true
        /// if the weekend should be hidden, false otherwise. This field 
        /// defaults to false.
        /// </summary>
        public bool HideWeekend
        {
            get { return this._hideWeekend; }
            set { this._hideWeekend = value; }
        }

        /// <summary>
        /// Override the default index for Saturdays.
        /// </summary>
        /// <remarks>This option is provided for internationalization options.</remarks>
        public int Saturday 
        {
            get { return this._saturday; }
            set { this._saturday = value; }
        }


        /// <summary>
        /// Override the default index for Sundays.
        /// </summary>
        /// <remarks>This option is provided for internationalization options.</remarks>
        public int Sunday 
        {
            get { return this._sunday; }
            set { this._sunday = value; }
        }

        /// <summary>
        /// Render the day on the calendar with the information provided.
        /// </summary>
        /// <param name="cell">The cell in the table.</param>
        /// <param name="day">The calendar day information</param>
        protected override void OnDayRender(TableCell cell, CalendarDay day)
        {
            // If this is a weekend day and they should be hidden, remove
            // them from the output
            if (day.IsWeekend && this._hideWeekend)
            {
                day = null;
                cell.Visible = false;
                cell.Text = string.Empty;
            }
            // Call the base render method too
            base.OnDayRender(cell, day);
        }

        /// <summary>
        /// Render the calendar to the HTML stream provided.
        /// </summary>
        /// <param name="html">The output control stream to write to.</param>
        protected override void Render(HtmlTextWriter html)
        {
            // Setup a new HtmlTextWriter that the base class will use to render
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter calendar = new HtmlTextWriter(sw);
            // Call the base Calendar's Render method allowing OnDayRender() 
            // to be executed.
            base.Render(calendar);
            // Check to see if we need to remove the weekends from the header,
            // if we do, then remove the fields and use the new verison for
            // the output. Otherwise, just use what was previously generated.
            if (this._hideWeekend && this.ShowDayHeader)
            {
                // Load the XHTML to a XML document for processing
                XmlDocument xml = new XmlDocument();
                xml.Load(new StringReader(sw.ToString()));
                // The Calendar control renders as a table, so navigate to the
                // second TR which has the day headers.
                XmlElement root = xml.DocumentElement;
                XmlNode oldNode = root.SelectNodes("/table/tr")[1];
                XmlNode sundayNode = oldNode.ChildNodes[this.Sunday];
                XmlNode saturdayNode = oldNode.ChildNodes[this.Saturday];
                XmlNode newNode = oldNode;
                newNode.RemoveChild(sundayNode);
                newNode.RemoveChild(saturdayNode);
                root.ReplaceChild(oldNode, newNode);
                // Replace the buffer
                html.WriteLine(root.OuterXml);
            }
            else
            {
                html.WriteLine(sw.ToString());
            }
        }
    }
}
Rob
Just a small comment that when I implemented this, the day headings ran from Tuesday to Saturday. The trick for me was to change the line: XmlNode sundayNode = oldNode.ChildNodes[0]; to: XmlNode sundayNode = oldNode.ChildNodes[5]; Possibly due to me being in the UK?
Brian
@Brian - That would most likely be the case, I'm in the United States which uses Sunday as the first day of the week for the calendar. I didn't include anything in the way of localization logic in the code so that might be something for me to do at some point.
Rob
@Joel - Just implemented this and tested in IE, predictably it doesn't appear as one would hope. The weekday headers and day cells all render at 5/7th's of the width of the calendar. This is due to the two day columns that were removed. I have a fix for this (replace any 'width:14%' in the xml to 'width:20%') and would like to share my solution with others. What's the best way for me to go about this?
Brian
@Brian - I wrote the original modification as an example to build off of as opposed to a full production version. Your best best would likely to be to answer this question with your version or you could do a blog entry somewhere and post up a link.
Rob