7.17. Adding a Calendar Control

The Calendar control is the most visually elegant of the Web Forms controls. The SelectionMode property can be set in one of four modes:

  1. None: The user can page through the Calendar by month but cannot make a selection.

  2. Day: The user can page through and select any day of the Calendar by month.

  3. DayWeek: The user can page through the Calendar by month. The user can either click on an individual day or select a week by clicking on an arrow placed in the leftmost column of the week.

  4. DayWeekMonth: The same as DayWeek, except that the user can also select the entire month by clicking on the double array in the leftmost column of the row holding the days of the week.

Clicking on a day, a week arrow, or a month double arrow (see Figure 7.7) raises the SelectionChanged event—unless the item is already marked as selected. The SelectedDate property refers to a DateTime object that is either the date selected in Day mode or the first date in the range of either the week or the month. For example, here is our event handler for the Calendar control when the user is able to select only a day:

private void
Calendar1_SelectionChanged(object s, EventArgs e)
{
    Label2.Text = Calendar1.SelectedDate.ToLongDateString();
}

The SelectedDates property contains the collection of the dates for the selected week or month. For example, to display the first and last dates selected, we would use either SelectedDate or SelectedDates[0] for the first date, and the last SelectedDates element for the last date in the selected range. Here is the code to do that, with the values placed in a pair of labels:

private void Calendar1_SelectionChanged(object s, EventArgs e)
{
    // really should go in Page_Load()
    Label1.Text =
           Calendar1.TodaysDate.ToLongDateString();
    if ( Calendar1.SelectedDates.Count > 1 )
    {
       int last = Calendar1.SelectedDates.Count-1;
       Label3.Text =
            Calendar1.SelectedDates[last].ToLongDateString();
    }
    else Label3.Text = null;

    Label2.Text = Calendar1.SelectedDate.ToLongDateString();
}

The TodaysDate property of the Calendar control represents the current calendar date. By default, it is set to the date of the server on which the page runs. Alternatively, we can assign it any DateTime object.

Nearly every aspect of the appearance of the Calendar control can be modified through the appearance and style categories of Calendar properties. In addition, a set of predefined calendar formats is available. Click on the AutoFormat link at the very bottom of the Calendar Properties window to select from among them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.232