SIGN UP MEMBER LOGIN:    
ARTICLE

Calendar Control in ASP.NET 3.5

Posted by Raj Kumar Articles | ASP.NET Programming September 04, 2008
This article shows how to use a Calendar control in ASP.NET 3.5 and Visual Studio 2008.
Reader Level:
Download Files:
 

ASP.NET 3.5 Calendar control creates a functionally rich and good looking calendar box that shows one month at a time. The user can move from month to month, select a date, and even select a range of days. The calendar control has many properties like you can change foreground color , background color, font, title, format of date, currently selected date and so on.

Use the Calendar control to display a single month of a calendar on a Web page. The control allows you to select dates and move to the next or previous month. The Calendar control supports all the System.Globalization. Calendar types in the System.Globalization namespace. Apart from the Gregorian calendar, this also includes calendars that use different year and month systems, such as the Hjiri calendar.  You can specify whether the Calendar control allows you to select a single day, week, or entire month by setting the SelectionMode property. By default, the control displays the days of the month, day headings for the days of the week, a title with the month name and year, links for selecting individual days of the month, and links for moving to the next and previous month. You can customize the appearance of the Calendar control by setting the properties that control the style for different parts of the control. The following table lists the properties that specify the style for the different parts of the control.

Note: The important Calendar event is SelectionChanged which fires every time a user clicks a date. Here is a basic event handler that responds to the SelectionChanged event and displayes the selected date.

There are nine events in calendar control.

  1. DayRender - Occurs when each day is created in the control hierarchy for the Calendar control.

  2. SelectionChanged - Occurs when the user selects a day, a week, or an entire month by clicking the date selector controls.

  3. VisibleMonthChanged - Occurs when the user clicks on the next or previous month navigation controls on the title heading.

  4. DataBinding - Occurs when the server control binds to a data source.

  5. Disposed - Occurs when a server control is released from memory, which is the last stage of the server control lifecycle when an ASP.NET page is requested.

  6. Init - Occurs when the server control is initialized, which is the first step in its lifecycle.

  7. Load - Occurs when the server control is loaded into the Page object.

  8. PreRender - Occurs when the server control is about to render to its containing Page object.

  9. Unload - Occurs when the server control is unloaded from memory.

Here is ASP.NET code that creates a calendar control. And the selection changed event is below that.

<h2>Calendar Control Example</h2>
            <asp:Calendar ID="Calendar1" runat="server" ForeColor="Orange" 
                 
 BackColor="Gray" ondayrender="Calendar1_DayRender"             
                  onselectionchanged
="Calendar1_SelectionChanged"></asp:Calendar>
           
<br />
           
<asp:Label ID="DatesLabel" runat="server"></asp:Label>

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    
{
  
         
DatesLabel.Text = "You selected these dates : <br />";
         
foreach (DateTime dt in Calendar1.SelectedDates)
         
{
               
DatesLabel.Text += dt.ToLongDateString() + "<br />";
         
}

    }

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    
{
           
if (e.Day.IsWeekend)
           
{

              
e.Cell.BackColor = System.Drawing.Color.Green;
               
e.Cell.ForeColor = System.Drawing.Color.Yellow;
              
e.Day.IsSelectable = false;

            
}

    }

Here is Output:


Figure 1.

After selection any date:


Figure 2.

 

This is it.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi, In calendar control on click of any date have to cal pop up screen. Can you help me how to do this?

Posted by Revathi Murugesan May 02, 2011

sir, can i use .net calender control as popup? If yes the how? giv me sample. Thank you

Posted by naresh karankal Dec 29, 2010

Hi this is zakir and i visited ur site for knowing ab8 calendar controls for .net 3.5 but the thing was ur site is very slow and its taking hell lot of time to load dont know wat is the problem with ur site....apart frm that ya thnx for the information and the article helped me a lot...thnx ones again and plz try to make ur site bit faster at least.

Posted by zakir Jan 19, 2010

You have to store selected date in session or viewstate first and after that convert this date according to your data fields. now you can get data according to selected date.

Posted by Raj Kumar Mar 18, 2009

You have to store selected date in session or viewstate first and after that convert this date according to your data fields. now you can get data according to selected date.

Posted by Raj Kumar Mar 18, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor