2012-08-31 55 views
1

在設計中,我有兩個textbuttons,兩個imagebuttons和兩個imagebuttons日曆單擊事件 在頁面加載我顯示在這兩個文本框中的今天日期 Page_load看起來像這樣;在asp.net中的日期檢查

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

現在我改變txttodate才值(TODATE比從日以上),然後單擊應該顯示「從日期應比迄今爲止更大的」 我寫的imagebutton1和2點擊事件按鈕消息如下

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 

和日曆SelectionChanged事件

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: 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 

起始日期必須大於日期較小

請hjelp我 感謝所有

回答

1

Althoug h我不太清楚,如果我完全理解你的問題。我不認爲它打算比較兩個字符串? 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"); 
    }