YouTip LogoYouTip

Prop Webcontrol Calendar Selectionmode

## ASP.NET Calendar SelectionMode Property The `SelectionMode` property is a member of the `System.Web.UI.WebControls.Calendar` class. It is used to specify or determine how a user can select dates on an ASP.NET Calendar control. --- ## Definition and Usage The `SelectionMode` property controls the date selection capabilities of the Calendar control. By modifying this property, you can restrict users to selecting a single day, allow them to select an entire week or month, or disable date selection entirely. When selection is enabled, the Calendar control renders selector columns and rows containing links that allow users to make their selections. --- ## Syntax ```xml ``` ### Property Values | Value | Description | | :--- | :--- | | **`None`** | Date selection is completely disabled. No selection links are rendered, and the user cannot select any dates. | | **`Day`** | Default value. The user can select a single day. Each day number is rendered as a link. | | **`DayWeek`** | The user can select a single day or an entire week. An extra column containing week selection links (`>`) is rendered on the left side of the calendar. | | **`DayWeekMonth`** | The user can select a single day, an entire week, or the entire month. In addition to the week selection links, a month selection link (`>>`) is rendered in the top-left corner of the calendar. | --- ## Code Examples ### Example 1: Basic Declarative Markup The following example demonstrates how to set the `SelectionMode` property to `DayWeekMonth` in your `.aspx` markup. This configuration allows users to select a single day, a full week, or the entire month. ```xml
``` ### Example 2: Handling Selection Events in Code-Behind When a user selects a date, week, or month, the `SelectionChanged` event is raised. You can access the selected dates using the `SelectedDates` collection property. #### ASPX Markup: ```xml <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CalendarDemo.aspx.cs" Inherits="YourApp.CalendarDemo" %> Calendar SelectionMode Example

Select a Date, Week, or Month:


``` #### C# Code-Behind: ```csharp using System; using System.Web.UI.WebControls; namespace YourApp { public partial class CalendarDemo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Initialization logic (if any) } protected void Calendar1_SelectionChanged(object sender, EventArgs e) { if (Calendar1.SelectedDates.Count > 0) { if (Calendar1.SelectedDates.Count == 1) { LabelStatus.Text = "You selected a single day: " + Calendar1.SelectedDate.ToShortDateString(); } else { DateTime startDate = Calendar1.SelectedDates; DateTime endDate = Calendar1.SelectedDates[Calendar1.SelectedDates.Count - 1]; LabelStatus.Text = string.Format( "You selected a range from {0} to {1} (Total: {2} days).", startDate.ToShortDateString(), endDate.ToShortDateString(), Calendar1.SelectedDates.Count ); } } } } } ``` --- ## Key Considerations 1. **Performance and ViewState**: When `SelectionMode` is set to `DayWeek` or `DayWeekMonth`, selecting a range of dates populates the `SelectedDates` collection. This collection is stored in the page's ViewState. Selecting large ranges (like an entire month) will slightly increase the ViewState size. 2. **Styling Selectors**: You can customize the appearance of the week and month selection links using the `SelectorStyle` property of the Calendar control. 3. **Default Behavior**: If the `SelectionMode` property is not explicitly defined, it defaults to `CalendarSelectionMode.Day`.
← Prop Webcontrol Calendar SelecProp Webcontrol Calendar Selec β†’