1

在设计中,我有 2 个文本框、2 个图像按钮和 1 个日历用于两个图像按钮单击事件在页面加载中,我在两个文本框中显示今天的日期 Page_load 看起来像这样;

txtfrdate.Text = Convert.ToDateTime(Today()).ToString("dd/MM/yyyy")
txttodate.Text = Convert.ToDateTime(Today()).ToString("dd/MM/yyyy")

现在我只更改 txttodate 的值(todate 大于 from date)然后单击按钮消息应该显示“From Date 应该大于 to date”我编写了 imagebutton1 和 2 click 事件如下

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
  Calendar1.Visible = True
  Calendar1.SelectedDates.Clear()
  Session("click") = 1
End Sub

Protected Sub ImageButton2_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
    Calendar1.Visible = True
    Calendar1.SelectedDates.Clear()
    Session("click") = 2
End Sub

并在日历选择更改事件

Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
    Select Case (Session("click"))
        Case 1 : txtfrdate.Text = Convert.ToDateTime(Calendar1.SelectedDate.ToShortDateString).ToString("dd/MM/yyyy")
            Session("fd") = Calendar1.SelectedDate
        Case 2 : txttodate.Text = Convert.ToDateTime(Calendar1.SelectedDate.ToShortDateString).ToString("dd/MM/yyyy")
            Session("td") = Calendar1.SelectedDate
    End Select
    Calendar1.Visible = False

    Label4.Visible = False
End Sub

我在点击查看点击按钮后检查日期如下

If txtfrdate.Text = "" Then
    Label4.Visible = True
    Label4.Text = "Select From Date"
ElseIf txttodate.Text = "" Then
    Label4.Visible = True
    Label4.Text = "Select To Date"
ElseIf txtfrdate.Text > txttodate.Text Then
    Label4.Visible = True
    Label4.Text = "From Date Must be Smaller than To Date"

实际上我的问题是当我只更改 txttodate 并且 todate 大于 from date 但仍然显示 From date 应该大于 to date 的消息“我的输出显示

From Date: 31/08/2012  - i am not changing current date during page load
To Date: 29/09/2012 - changing by selecting the calendar message is showing

起始日期必须小于截止日期

请帮助我谢谢大家

4

1 回答 1

1

虽然我不太确定我是否完全理解你的问题。而且我认为它不是为了比较两个字符串?txtfrdate.Text > txttodate.text比较字符串值并且不比较 dateTime 对象

使用此示例并根据您的要求对其进行调整以检查 dateTime 值。

    DateTime dtFrom = DateTime.Now;
    DateTime dtTo = DateTime.Now.Subtract(new TimeSpan(5, 0,0,0));

    if (dtFrom > dtTo)
    {
        Debug.WriteLine("Invalid To-Date");
    }
    else
    {
        Debug.WriteLine("OK");
    }
于 2012-08-31T10:20:01.960 回答