How to make a extender calendar unable to select a date base on a selected date?

320 views Asked by At

How can I make user unable to select an end date before the an start date?

<asp:Label ID="lblStartDate" runat="server" Text="Start Date: " CssClass="labelClass"></asp:Label>
<asp:TextBox ID="tbStartDate" runat="server" ReadOnly="True"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalExtStartDate" runat="server" Format="dd/MM/yyyy" TargetControlID="tbStartDate" />



<asp:Label ID="lblEndDate" runat="server" Text="End Date: " CssClass="labelClass"></asp:Label>
    <asp:TextBox ID="tbEndDate" runat="server" ReadOnly="True"></asp:TextBox>
    <ajaxToolkit:CalendarExtender ID="CalExtEndDate" runat="server" TargetControlID="tbEndDate" Format="dd/MM/yyyy" />

In my .aspx.cs I've got but it still doesn't work.

    if (!IsPostBack)
        {
            CalExtStartDate.StartDate = DateTime.Now.AddDays(-7);

            CalExtEndDate.StartDate = CalExtStartDate.SelectedDate;

        }
1

There are 1 answers

2
user1498969 On

I believe that you can use a compare validator paired with 2 required field validators (one for each date textbox):

<asp:TextBox ID="tbStartDate" runat="server" ReadOnly="True"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="End Date required" ID="requiredDate1" ControlToValidate="tbStartDate" />

<asp:TextBox ID="tbEndDate" runat="server" ReadOnly="True"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="Start Date required" ID="requiredDate2" ControlToValidate="tbEndDate" />

<asp:CompareValidator runat="server" ID="compareValidator1" ErrorMessage="End date must be after start date" ControlToCompare="tbStartDate" ControlToValidate="tbEndDate" Operator="GreaterThan" Type="Date" />