Ask the Expert

How do I write a calendar control that allows specification of start/end date from the database?

I want to write my own calendar control like where I will specify start date and end date from database. Is this possible? If yes, can you send me code?
The procedure basically involves inheriting from Calendar and attaching to the SelectionChanged and VisibleMonthChanged events, where you can compare the new values with your DB-valid dates and set the VisibleDate/SelectedDate properties accordingly. You can also catch the DayRender and make the invalid dates unselectable, such as:
public class MyCalendar : Calendar
{
 //Get the dates from a db connection.
 DateTime _last = new DateTime(2003, 7, 30); 
 DateTime _first = new DateTime(2003, 4, 1);

 public MyCalendar()
 {
  base.SelectionChanged += new EventHandler(OnDateChange);
  base.VisibleMonthChanged += new MonthChangedEventHandler
(OnMonthChanged);
base.DayRender += new DayRenderEventHandler(OnDayRender);
 }

 private void OnDateChange(object sender, EventArgs e)
 {
  //Check valid date or show last/first valid ones.
  if (SelectedDate > _last)
  {
   SelectedDate = _last;
  }
  else if (SelectedDate < _first)
  {
   SelectedDate = _first;
  }
 }

 private void OnMonthChanged(object sender, MonthChangedEventArgs e)
 {
  //Check valid month or show last/first valid ones.
  if (e.NewDate > _last || e.NewDate < _first)
  {
   VisibleDate = e.PreviousDate;
  }
 }

 private void OnDayRender(object sender, DayRenderEventArgs e)
 {
  if (e.Day.Date > _last || e.Day.Date < _first)
  {
   e.Day.IsSelectable = false;
  }
 }
}

This was first published in June 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: