Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

Using Calendar Control in ASP.NET

Calendar is one of the Rich Web Controls available in ASP.NET. Calendar is used to display a one-month calendar. Users can use this control to view dates or select a specific day, week, or month.

 

The following table lists some properties of the Calendar control:

 


 


 

 

 

 PROPERTY

DESCRIPTION

DayNameFormat

This property is used to specify the name format of the days of a week.

   

VisibleDate

This property is used to specify the month to be displayed in a Calendar control. The property is updated after the VisibleMonthChanged event is raised.

   

FirstDayOfWeek

To specify the day of the week to be displayed in the first column of the Calendar control

SelectedDate

It is used to represent the date selected in a Calendar control.

SelectionMode

It is used to specify whether a user can select a day, a week, or a month. The default value is a day.

LCID

Returns the culture identifier for the instance of the CultureInfo class. Every culture has an identifier. For e.g., 0x0407 is the identifier for "de-DE" culture

The following table lists some of the events of the Calendar control:

 EVENT

DESCRIPTION

DayRender

This event is raised before each day call is rendered on the Calendar control.

SelectionChanged

This event is raised when the user select a new date, week or month.

VisibleMonthChanged

This event is raised when user clicks on the month's navigation control.

The following table lists some methods of the Calendar control:

METHODS

DESCRIPTION

OnDayRender

Used to raise the DayRender event.

OnSelectionChanged

Used to raise the SelectionChanged event.

Using Calendar Control - Practical Example

I have used the calendar control, in this project to calculate the number of days of service of an employee. To do this perform the following steps:

  1. Open a new ASP.NET web application.

  2. Add a label and set its text to "Your hire date", a textbox, a link button, a calendar control and another label to display the output message.

  3. You can set the format of your calendar, by right clicking the calendar control in design view and clicking on "autoformat". Make the settings according to your preferences.

  4. The user interface looks like as follows. As you click the Calendar link button, a calendar window appears.

    5.  And then the user has to select his hire date, which is assigned as the text property of the text box. And when you click on the continue button, this is compared to the current date to calculate the service period of an employee, as shown in the figure.

The main functionality of calculating the service duration is performed by the following line of code:

private void Button1_Click(object sender, System.EventArgs e)
{
  TimeSpan service=DateTime.Now.Subtract (c.SelectedDate );
  lbl.Text="You have worked "+service.Days+" days in our company";
}

There's much more to the list of properties and functionalities that you can use in conjunction with the Calendar control to enhance your application. You can download Sample Caledar Control project, used in this tutorial.


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |