Jes Sie

Jes Sie

  • 736
  • 1.2k
  • 284.3k

Restrict Selecting Date from Calendar Control

Feb 26 2017 11:38 PM
I have 2 textboxes, the first textbox is a date from the database and the second textbox is asp:calendar. Now, this is what I want to achieve: Textbox2 cannot select a date older that what is in textbox1. Below is my code but it is not working.
 
html and c# code:
 
  1.  <ajaxToolkit:CalendarExtender ID="calFirst" runat="server"  
  2.                 TargetControlID="TextBox1"  
  3.                 CssClass="MyCalendar"  
  4.                 Format="MMMM d, yyyy" />  
  5.   
  6. <asp:TextBox ID="TextBox1" runat="server">asp:TextBox>  
  7.   
  8. <br>  
  9. <asp:TextBox ID="TextBox4" runat="server">asp:TextBox>  
  10.   
  11.             <asp:ImageButton ID="ImageButton1" runat="server" Height="17px"  
  12.                 ImageUrl="~/img/calendar.png" OnClick="ImageButton1_Click" Width="21px" />  
  13.             <asp:Calendar ID="Calendar1" runat="server"  
  14.                 OnSelectionChanged="Calendar1_SelectionChanged" 
  15. OnDayRender="Calendar1_DayRender" Visible="False">asp:Calendar>  
    1. protected void Calendar1_SelectionChanged(object sender, EventArgs e)  
    2.         {  
    3.             TextBox4.Text = Calendar1.SelectedDate.ToShortDateString();  
    4.             Calendar1.Visible = false;  
    5.         }  
    6.   
    7.         protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)  
    8.         {  
    9.             DateTime date = Convert.ToDateTime(TextBox1.Text);  
    10.             if (e.Day.Date > date)  
    11.             {  
    12.                 e.Cell.Enabled = false;  
    13.                 e.Day.IsSelectable = false;  
    14.             }  
    15.         }  
    16.   
    17.         protected void ImageButton1_Click(object sender, ImageClickEventArgs e)  
    18.         {  
    19.             Calendar1.Visible = true;  
    20.         }  
                     

Answers (6)