繁体   English   中英

CompareValidator无法正常工作

[英]CompareValidator not working correctly

我宁愿使用asp.net验证控件,因为我目前在同一视图中还有其他验证控件。 我需要在验证摘要中显示错误消息。

我有两个文本框,我需要确保文本框A比文本框B少。

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

  • ControlToCompare:文本框B
  • ControlToValidate:textboxA
  • 接线员:GreaterThan /还尝试了LessThan
  • 类型:日期

这是问题所在:

  • 当我在textboxA中提供时间并移至textboxB时,将显示验证错误。 我以为我的if语句可以解决此问题,但不能解决。

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

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

在此先感谢您的帮助。

克莱尔

我认为您需要使用CustomValidator,并且仍将允许您使用ValidationSummary。

看这里:

如何在没有日期的时间中使用CompareValidator?

如果我对您的理解正确,则需要比较两个日期。

我在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

您是否要尝试将if语句更改为:

if (!string.IsNullOrEmpty(textboxA.Text) && !string.IsNullOrEmpty(textboxB.text))

如果要在服务器端比较两个日期或时间,请使用此解决方案

DateTime dt1 = Convert.ToDateTime(TextBoxA.Text);
DateTime dt2 = Convert.ToDateTime(TextBoxB.Text);

int result = dt1.CompareTo(dt2)

我将使用CustomValidator( http://msdn.microsoft.com/zh-cn/library/9eee01cx ( VS.71 ) .aspx )。 顺便说一句:您为什么直接致电验证者?

timeCompareValidator.Validate();

通常,在每个Button触发的Page.Validate()过程中评估验证器(如果CausesValidation未设置为false)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM