首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CompareValidator不能正常工作

CompareValidator不能正常工作
EN

Stack Overflow用户
提问于 2009-09-25 08:47:32
回答 4查看 3.7K关注 0票数 2

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

我有两个文本框,我需要确保textboxA是LessThan textboxB。

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

  • ControlToCompare: textboxB
  • ControlToValidate: textboxA
  • 操作员: GreaterThan /也尝试过LessThan
  • 类型:日期

以下是问题所在:

  • 当我在textboxA中提供时间并移到textboxB上时,将显示验证错误。我以为我的if语句能解决这个问题,但事实并非如此。

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

代码语言:javascript
复制
        if(String.IsNullOrEmpty(textboxB.Text))
        {
            Debug.Write("Valid");
            timeCompareValidator.IsValid = true;    
        }

提前感谢您的帮助。

克莱尔

EN

回答 4

Stack Overflow用户

发布于 2009-09-25 08:56:07

如果我没听错,你需要比较两个日期。

我在msdn上找到了这段代码

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2009-09-25 09:02:42

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

代码语言:javascript
复制
if (!string.IsNullOrEmpty(textboxA.Text) && !string.IsNullOrEmpty(textboxB.text))
票数 0
EN

Stack Overflow用户

发布于 2009-09-25 09:19:14

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

代码语言:javascript
复制
DateTime dt1 = Convert.ToDateTime(TextBoxA.Text);
DateTime dt2 = Convert.ToDateTime(TextBoxB.Text);

int result = dt1.CompareTo(dt2)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1476161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档