简体   繁体   English

比较ASP日历中的两个日期

[英]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. 我是ASP的菜鸟,我想从ASP日历中选取一个选定的日期,并将其保存在文本框中,或者将其与第二个选定的日期进行比较,以使第二个更大。 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. 我确实做了搜索,但他们使用的是JavaScript,Java或其他我不知道的语言。

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";
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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