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

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DissableDates.aspx.cs" Inherits="DissableDates" %>  
  2. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6. <title>dissable past or future dates</title>  
  7. <style type="text/css">  
  8. .clndrdiv  
  9. {  
  10. background-color:Silver;  
  11. border-style:solid;  
  12. width:250px;  
  13. height:100px;    
  14. background-position:center;  
  15. }  
  16. </style>  
  17. </head>  
  18. <body>  
  19. <form id="form1" runat="server">  
  20. <asp:ScriptManager ID="ScriptManager1" runat="server">  
  21. </asp:ScriptManager>  
  22. <div class="clndrdiv"  align="center">  
  23. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  24. <asp:CalendarExtender ID="Calendar1" runat="server"   
  25. Enabled="True" TargetControlID="TextBox1" Format="dd/MM/yyyy" ></asp:CalendarExtender>  
  26. </div>  
  27. </form>  
  28. </body>  
  29. </html> 
 
cs File
 
  1.  using System;  
  2. public partial class DissableDates : System.Web.UI.Page  
  3. {  
  4. protected void Page_Load(object sender, EventArgs e)  
  5. {  
  6. Calendar1.StartDate = DateTime.Now; //dissabling past dates  
  7. // Calendar1.StartDate = DateTime.Now.AddMonths(1);  // Start Calneder after one month from Present Date  
  8. //Calendar1.EndDate = DateTime.Now;                 // dissabling Future Dates   
  9. /* Calendar1.StartDate = DateTime.Now; 
  10. Calendar1.EndDate = DateTime.Now;*/     //use these two lines at a time to allow only present date    
  11. }