Disable Future and Past Date Of Ajax Calendar in Asp.net

Before Ajax Control Toolkit 4.0 or Any other Calender such as Asp.net Calender Control it is very Lengthy and Long Process to disable the past or future dates from Calendar. because to perform this type of task we need to write more than a hundred lines of code in JavaScript or any other scripting Language.

But now by using the Ajax control Toolkit 4.0 you can perform the same task in just one line of code..!

Applicable

  • It is useful when their is requirement of only allow Present date or allow only limited future date from present date of calender.
  • It is useful in a application such as Travel Booking Application or Daily Transaction Application or according to your requirement as you wish.

Note

  • You need an Ajax Control Toolkit 4.0 and above,its only works in this version
  • Don't forget to add the Ajax control toolkit assembly reference at the top of the .aspx page source code 
  • Don't forget to use either Script Manager or  ToolScript Manager in a .aspx page because whenever you use any Ajax control you need to use one of them to handle Script .

.aspx source code
 

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DissableDates.aspx.cs" Inherits="DissableDates" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>dissable past or future dates</title>
    <style type="text/css">
    .clndrdiv
    {
   background-color:Silver;
   border-style:solid;
   width:250px;
   height:100px;  
   background-position:center;
 
 
   }
 
 
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div class="clndrdiv"  align="center">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:CalendarExtender ID="Calendar1" runat="server" 
            Enabled="True" TargetControlID="TextBox1" Format="dd/MM/yyyy" ></asp:CalendarExtender>
    </div>
    </form>
</body>
</html>

cs File
 

 using System;

public partial class DissableDates : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       Calendar1.StartDate = DateTime.Now; //dissabling past dates

       // Calendar1.StartDate = DateTime.Now.AddMonths(1);  // Start Calneder after one month from Present Date
        //Calendar1.EndDate = DateTime.Now;                 // dissabling Future Dates 
       /* Calendar1.StartDate = DateTime.Now;
        Calendar1.EndDate = DateTime.Now;*/     //use these two lines at a time to allow only present date  

    }
}

 
Ebook Download
View all
Learn
View all