检查一次比另一次大

时间:2014-10-02 14:00:51

标签: asp.net vb.net visual-studio-2013

我使用自定义验证器检查一个时间跨度大于另一个。

From:<asp:DropDownList runat="server" ID="ddlFromTime">                                </asp:DropDownList>
To:<asp:DropDownList runat="server" ID="ddlToTime">
</asp:DropDownList>

<asp:CustomValidator runat="server" OnServerValidate="ValidateTime" ErrorMessage="Please Select a time greater than from time"></asp:CustomValidator>

Protected Sub ValidateTime(source As Object, args As ServerValidateEventArgs)
        Dim fromTime As TimeSpan = TimeSpan.Parse(ddlFromTime.SelectedValue)
        Dim toTime As TimeSpan = TimeSpan.Parse(ddlToTime.SelectedValue)

        args.IsValid = fromTime > toTime
    End Sub

我希望能够验证我是否选择了#34; From Time&#34;这大于&#34;到时间&#34;。

e.g。我选择了一个从时间上午11点到时间上午9点,这是错误的。

我如何验证这个?

2 个答案:

答案 0 :(得分:1)

args.IsValid = fromTime > toTime似乎不正确,如果ToTime大于,则有效。

那不就是反过来吗?

args.IsValid = fromTime < toTime

答案 1 :(得分:1)

“上午11点”并不代表时间跨度,而是时间。您应该解析DateTime个对象而不是TimeSpan

Dim fromTime As DateTime = DateTime.Parse(ddlFromTime.SelectedValue)
Dim toTime As DateTime = DateTime.Parse(ddlToTime.SelectedValue)

args.IsValid = fromTime < toTime