Calendar Control in ASP.NET

In this article we will learn how to use Calendar control in ASP. NET.
  • 3170

In this article we will learn how to use Calendar control in ASP. NET.

Calendar control:

The calendar control is a very important control. This Web server control displays a single month calendar that allows the user to select a date and move to the next or previous month. Users can use this control to view dates or select a specific day, week, or month.

Properties: These are the following properties of the Calendar control.

cal1.gif

figure 1.

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.

Event: These are the following event of the Calendar control.

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.

For 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:

Drag a label and set its text to "Your current date", a textbox and a calendar control and another label to display the output message. The form looks like this.

cal2.gif

Figure 2.

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.

Now double click on the button and add the following code.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Dim service As TimeSpan = DateTime.Now.Subtract(Calendar1.SelectedDate.ToString())

 

        lbl.Text = "You have worked " + service.Days.ToString() & " days in our company"

    End Sub


Now save and run the application.
cal3.gif

Figure 3.

Now enter the current date in the TextBox and then select Calendar date.

cal4.gif

Figure 4.

Now click on the Button control. The output will be display days.

cal5.gif

Figure 5. 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.