简体   繁体   中英

Compare two dates in ASP calender

I am a noob at ASP, I am trying to take take a selected date from an ASP calendar and save that in a textbox or something that I can compare with the second selected date so that the second is larger. I'm not sure if it is possible while just using one calendar. I tried but do not know how to save the first date collection for comparison. I tried both ways but failed miserable. I did do a search but they are using javascript, or java and other languages I do not know.

What I want to do: I am trying to take two separate inputted dates from user. when 1st date is inputted, store in something, then validate that the user selects a date after the 1st selected date. If not return error message

<asp:TextBox ID="response" runat="server" />                       
                                <asp:TextBox ID="caldate1" runat="server" />
                                <asp:TextBox ID="caldate2" runat="server" />

                                <asp:CompareValidator ID="calvalidae" runat="server" ControlToCompare="caldate1" ErrorMessage="Date should be later than first date" Type="Date" operator="GreaterThan" ValueToCompare="caldate2"></asp:CompareValidator>
                                <asp:Calendar ID="cal1" runat="server"></asp:Calendar>
                                <asp:Calendar ID="cal2" runat="server" SelectionMode="Day" OnSelectionChanged="cal1_SelectionChanged" ></asp:Calendar>

//serverside

protected void cal1_SelectionChanged(object sender, EventArgs e)
    {
        caldate1.Text = cal1.SelectedDate.ToShortDateString();
        if (cal1.SelectedDate.Date > cal2.SelectedDate.Date)
        {
            caldate1.Text = "You selected ";
            caldate1.Text += cal1.SelectedDate.ToShortDateString();
        }
        else
        {
            caldate1.Text = "Select a valid date";
        }

    }

If you just want to compare the dates in code behind do the below

Markup




            <asp:CompareValidator ID="calvalidae" runat="server" 
                                  ControlToValidate ="caldate1"  ValueToCompare="text" ControlToCompare="caldate2"
                ErrorMessage="Date should be later than first date" Type="Date" 
                operator="GreaterThan" ></asp:CompareValidator><br/>                      
            <asp:Calendar ID="cal1" runat="server" OnSelectionChanged="cal1_SelectionChanged1"></asp:Calendar><br/>                      
            <asp:Calendar ID="cal2" runat="server" SelectionMode="Day" OnSelectionChanged="cal2_SelectionChanged" ></asp:Calendar><br/>                      
        </div>

Code Behind

    protected void cal1_SelectionChanged1(object sender, EventArgs e)
    {
        caldate1.Text = cal1.SelectedDate.ToShortDateString();
        IsValidDate();
    }

    protected void cal2_SelectionChanged(object sender, EventArgs e)
    {
        caldate2.Text = cal2.SelectedDate.ToShortDateString();
        IsValidDate();
    }


    private void IsValidDate()
    {
        response.Text = string.Empty;
        if (cal1.SelectedDate > cal2.SelectedDate)
        {
            response.Text = "Date should be later than first date";
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM