0

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;

        }
Vexasoe
  • 23
  • 6

1 Answers1

1

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" />
  • that's an answer. but I need to show that on the Calendar Extender GUI, it cancel out the past date... thanks anyway! – Vexasoe Aug 11 '16 at 19:51
  • 1
    You're welcome. It's probably best if you still use the validator anyway, even if you're looking to grey out the calendar. My experience with the ajax calendar have been that it's a good bit limited in what you can do with it. If you want to get really specific then you might want to look into integrating jquery calendars into your project. – user1498969 Aug 11 '16 at 20:18