是什么决定的顺序验证火? [英] What determines the order validators fire in?

查看:104
本文介绍了是什么决定的顺序验证火?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个自定义验证一个WebForm;一个验证字符串的日期格式(我不在乎什么格式,只要它可以转换),另一个以确保一个日期等于或大于另一个(只是无法得到比较验证器玩任何日期格式不错)。下面是它的外观:

I’ve got a webform with two custom validators; one to validate a string is of date format (I don’t care what format, so long as it can convert), and another to ensure that one date is equal to or greater than another (just couldn’t get the compare validator to play nice with any date format). Here’s how it looks:

<asp:TextBox ID="txtResourceStartDate" runat="server" CssClass="textBox mandatory dateField" />
<asp:CustomValidator ID="valResourceStartDateIsDate" runat="server" ControlToValidate="txtResourceStartDate" Display="None" ErrorMessage="Start date must be a valid date" OnServerValidate="Date_ServerValidate" />

<asp:TextBox ID="txtResourceEndDate" runat="server" CssClass="textBox mandatory dateField" />
<asp:CustomValidator ID="valResourceEndDateIsDate" runat="server" ControlToValidate="txtResourceEndDate" Display="None" ErrorMessage="End date must be a valid date" OnServerValidate="Date_ServerValidate" />

<asp:CustomValidator Display="None" Text="" ID="valForStartEndDate" runat="server"         OnServerValidate="ValidateStartEndDate" ErrorMessage="Last day must be greater than or equal to first day" />

和code明智的:

protected void Date_ServerValidate(object source, ServerValidateEventArgs args)
{
  DateTime outDate;
  args.IsValid = DateTime.TryParse(args.Value, out outDate);
}

protected void ValidateStartEndDate(object sender, ServerValidateEventArgs e)
{
  e.IsValid = DateTime.Parse(txtResourceEndDate.Text) >= DateTime.Parse(txtResourceStartDate.Text);
}

我的问题是,ValidateStartEndDate验证器的Date_ServerValidate验证之前,所以如果日期是无效的格式异常被抛出DateTime.Parse射击。显然,这验证可以检查有效日期解析之前,但我真的preFER有相应的消息离散验证。

The problem I have is that the ValidateStartEndDate validator is firing before the Date_ServerValidate validator so if the date is not valid a format exception is thrown on DateTime.Parse. Obviously this validator could check for a valid date before parsing but I’d really prefer to have a discrete validator with an appropriate message.

所以,问题是这样的;什么是确定的顺序与验证器火?除非我失去了一些东西,这不是在标签级别声明。谢谢!

So the question is this; what is determining the sequence with which the validators fire? Unless I’m missing something, this is not declared at the tag level. Thanks!

推荐答案

您不能指望一定的顺序,校验器将火还你不应该。你必须确保自己的顺序是无关紧要的。

You can't count on a certain sequence the validators will fire and also you shouldnt. You have to make sure for yourself that the order is irrelevant.

所以,你可以


  1. 检查有效日期
    同时与
    平等-大-检查。

  2. 首先打电话给你的则IsDate验证器的validate() - 函数,然后检查是否的IsValid

  3. 所有验证器被添加到Page.Validators收集和验证通过这个集合才能运行。如果你的逻辑真的应该靠这个顺序:更改ASPX页的验证顺序

有关页面验证一些有趣的相关信息: http://msdn.microsoft。 COM / EN-US /库/ aa479045.aspx

Some interesting infos about Page-Validation: http://msdn.microsoft.com/en-us/library/aa479045.aspx

这篇关于是什么决定的顺序验证火?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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