c# - CompareValidator 无法正常工作

标签 c# asp.net validation

<分区>

我更喜欢使用 asp.net 验证控件,因为我目前在同一个 View 中有其他验证控件。我需要在验证摘要中显示错误消息。

我有两个文本框,我需要确保 textboxA 小于 textboxB。

我使用了 CompareValidator 并将属性设置为:

  • 控件比较:文本框B
  • ControlToValidate: 文本框A
  • 运算符:GreaterThan/也试过 LessThan
  • 类型:日期

问题是:

  • 当我在 textboxA 中提供时间时 并移至 textboxB 进行验证 显示错误。我以为我的如果 声明会解决这个问题,但它 没有。

在“更新”按钮的点击事件中,我添加了以下代码,因为我只需要它来验证 textboxA/textboxB != null。

        if(String.IsNullOrEmpty(textboxB.Text))
        {
            Debug.Write("Valid");
            timeCompareValidator.IsValid = true;    
        }

在此先感谢您的帮助。

克莱尔

最佳答案

如果我没理解错的话,你需要比较两个日期。

I found this code on msdn :

DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;

if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";

Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
// The example displays the following output:
//    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM

关于c# - CompareValidator 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1476161/

相关文章:

c# - CheckBox 呈现为 Label

c# - 如何使 IDisposable 对象成为类变量?

c# 使用LiNQ删除Datatable中的多行

c# - USB 闪存驱动器模拟(如 cd/dvd rom 虚拟驱动器)-是否可能

c# - 像 Twitter 一样的实时更新

javascript:计算文本输入中的数字

java - 基于标准的字符串验证

c# - 验证 ASP.NET MVC 表单时的回发

c - 循环内的 fgets C

ASP.Net session 间事件