我是ASP的菜鸟,我试图从ASP日历中选择一个日期并将其保存在文本框中,或者我可以与第二个选定日期进行比较,以便第二个更大。我只是在使用一个日历时不确定是否可行。我试过但不知道如何保存第一个日期集合进行比较。我尝试了两种方法,但失败了。我确实进行了搜索,但他们使用的是javascript,或java和其他我不知道的语言。
我想做什么: 我想从用户那里拿两个单独的输入日期。当输入第一个日期时,存储在某个内容中,然后验证用户在第一个选择日期之后选择日期。如果没有返回错误消息
<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>
//服务器侧
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";
}
}
答案 0 :(得分:1)
如果您只想比较后面代码中的日期,请执行以下操作
<强>标记强>
<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>
代码背后
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";
}
}