Customised Calender Control in ASP.Net

This tutorial explains the ASP.NET Calender Control and how to implement it in C# code.
 
At the very first we will create an Empty ASP.NET Web application and add a simple webform to it. 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CalenderControlDemo.WebForm1" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.       
  13.         <asp:TextBox ID="TextBox1" runat="server" Height="28px" ReadOnly="True"></asp:TextBox>  
  14.    
  15.         <asp:ImageButton ID="ImageButton1" runat="server" Height="34px" ImageUrl="~/Images/Calendar-256.png" OnClick="ImageButton1_Click" style="margin-left: 0px" Width="33px" />  
  16.         <br />  
  17.         <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" OnDayRender="Calendar1_DayRender" OnSelectionChanged="Calendar1_SelectionChanged" ShowGridLines="True" Width="220px">  
  18.             <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />  
  19.             <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />  
  20.             <OtherMonthDayStyle ForeColor="#CC9966" />  
  21.             <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />  
  22.             <SelectorStyle BackColor="#FFCC66" />  
  23.             <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />  
  24.             <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />  
  25.         </asp:Calendar>  
  26.       
  27.     </div>  
  28.     </form>  
  29. </body>  
  30. </html>  
The preceding code Implements the front end of the application. 
 
Now let's explain it in detail. 
  1. Add a TextBox, Image button and a Calendar Control to the div element.

  2. Assign an image to the Image button to make it look nice. 

  3. Press the F7 function key to view the code behind file and paste the following code in it. Make sure that the project name should be the same as that of My Project, in other words CalenderControlDemo.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace CalenderControlDemo  
  9. {  
  10.     public partial class WebForm1 : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.             if (!IsPostBack)  
  15.             {  
  16.                 Calendar1.Visible = false;  
  17.             }  
  18.         }  
  19.   
  20.         protected void Calendar1_SelectionChanged(object sender, EventArgs e)  
  21.         {  
  22.             TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();  
  23.             Calendar1.Visible = false;  
  24.         }  
  25.   
  26.         protected void ImageButton1_Click(object sender, ImageClickEventArgs e)  
  27.         {  
  28.             if (Calendar1.Visible)  
  29.             {  
  30.                 Calendar1.Visible = false;  
  31.             }  
  32.             else  
  33.             {  
  34.                 Calendar1.Visible = true;  
  35.             }  
  36.         }  
  37.   
  38.         protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)  
  39.         {  
  40.             if (e.Day.IsWeekend || e.Day.IsOtherMonth)  
  41.             {  
  42.                 e.Day.IsSelectable = false;  
  43.                 e.Cell.BackColor = System.Drawing.Color.Gray;  
  44.             }  
  45.         }  
  46.     }  
  47. }   
  1.  At the very first, generate the Click Event of the Button Control.

  2. DayRender and SelectionChanged Event of the Calender Control.
  • In the Page_Load Event, if it is the Get request not the PostBack Request, we want to hide the calendar. So, we set the Visible property of the Calender control to false. 

  • When we click on the Button Control, the calender should be visible. So, we set the Visible property of the calender control to true. But this time we also want to check if the calender is already opened, so it should hide and vice versa. This will provide the ability to the user to show/hide the calendar.

  • Now, if we select something from the calendar, in other words we change the selection, the date should be entered into the TextBox in the Short Date format and at the same time the calendar should hide.

  • In the Day_Render event, we are checking whether the date belongs to the current month or the weekends, if so then these dates should not be selectable by making the IsSelectable property false.

  • Also, we changed the color of the cell to make it look more recognisable.
Go ahead and run the project, you will see the power of the Calender Control of ASP.NET.
 
Please comment if any queries.


Similar Articles