如何在没有日期的时间中使用CompareValidator? [英] How can I use CompareValidator for times without dates?

查看:98
本文介绍了如何在没有日期的时间中使用CompareValidator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在开始控件中输入 9:00 并在完成中输入 16:00 时;下面的代码无法通过验证.

When I enter 9:00 into the Start control, and 16:00 into Finish; the code below fails validation.

有人知道我可以将ASP.NET验证器用于没有日期的时间的方法吗?

Does anybody know a way I can use the ASP.NET validator for times without the dates?

Start <asp:TextBox ID="txtStart" runat="server" /> (hh:mm)
<br />
Finish <asp:TextBox ID="txtFinish" runat="server" /> (hh:mm)
<br />
<asp:CompareValidator
    id="cpvFinish"
    ControlToValidate="txtFinish"
    ControlToCompare="txtStart"
    Operator="GreaterThanEqual"
    Type="Date"
    Display="Static"
    EnableClientScript="true"
    ErrorMessage="Finish time must be later than the start time."
    runat="server" />

PS-我知道我可以轻松地使用CustomValidator,这似乎是该验证程序应该能够处理的事情.

PS-I know I can easily use CustomValidator instead, it just seems like something this validator should be able to handle.

推荐答案

这显然无法完成.

作记录;我使用了自定义验证器.

For the record; I used a custom validator.

这是我的自定义验证程序代码,以防万一(例如alhambraeidos)需要它.请注意,此代码将我的示例控件名称(txtStart& txtFinish)替换为我应用中的实际名称(txtReady& txtClose).

Here's my custom validator code in case anyone (i.e. alhambraeidos) needs it. Please note, this code replaces my sample control names (txtStart & txtFinish) with actual names from my app (txtReady & txtClose).

try
{
    // parse & compare dates
    string randomDt = "01/01/2000 ";
    args.IsValid = DateTime.Parse(randomDt + txtClose.Text) > DateTime.Parse(randomDt + txtReady.Text);
}
catch(Exception /*ex*/)
{
    // exception is assumed to be invalid times entered
    args.IsValid = false;
}

这篇关于如何在没有日期的时间中使用CompareValidator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆