本文介绍了CompareValidator 的间歇性错误 - ValueToCompare 属性为“"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 CompareValidator 的页面:

I have a page with a CompareValidator on it:

<asp:textbox id="txtResponseDate" runat="server" />
<asp:requiredfieldvalidator id="rfvResponseDate" runat="server"
                        controltovalidate="txtResponseDate"
                        display="Dynamic"
                        errormessage="Date is required."
                        setfocusonerror="true">
                    </asp:requiredfieldvalidator>
<asp:comparevalidator id="cmvDate" runat="server"
                        controltovalidate="txtResponseDate"
                        display="Dynamic"
                        errormessage="Date must not be before today."
                        operator="GreaterThanEqual"
                        setfocusonerror="true"
                        type="Date">
                    </asp:comparevalidator>

在后面的代码中,我们像这样设置 ValueToCompare 属性:

In the code behind, we set the ValueToCompare property like so:

If Not IsPostBack Then

    cmvDate.ValueToCompare = DateTime.Now.ToString("d")

End If

间歇性地(我们无法辨别模式),我们收到以下错误:

Intermittently (we can't discern a pattern), we get the following error:

'cmvDate' 的 ValueToCompare 属性的值 '' 无法转换为类型 'Date'."

"The value '' of the ValueToCompare property of 'cmvDate' cannot be converted to type 'Date'."

调用堆栈为:

at System.Web.UI.WebControls.CompareValidator.ControlPropertiesValid()
at System.Web.UI.WebControls.BaseValidator.get_PropertiesValid()
at System.Web.UI.WebControls.BaseValidator.Validate()
at System.Web.UI.Page.Validate()
at System.Web.UI.Page.Validate(String validationGroup)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)

我的第一个想法是日期格式有些奇怪,但我们使用的是英国日期,最后一次错误发生在 7 月 18 日,因此 ValueToCompare 设置为 18/07/2011.我已经使用调试器手动设置了它,它运行良好.关于为什么会发生这种情况,有人有什么好主意吗?

My first thought was something odd with the date format, but we're using UK dates, last error was on 18th July, so the ValueToCompare gets set to 18/07/2011. I've set this manually using the debugger, and it worked fine. Anybody any bright ideas as to why this may be happening?

  1. 我已经检查了代码 - 没有其他地方可以设置.
  2. 使用调试器,我在回发时仔细检查过,正如预期的那样,这个属性值被保留了.

推荐答案

此错误消息表示,在验证属性 ValueToCompare 时,您的 rangeValidator 本身(不是您正在验证的控件)未设置.如果您写了以下几行,则可能是这样:

This error message says, that in moment of validation property ValueToCompare of your rangeValidator itself (not the control you are validating) is not set. This can be if you wrote this lines:

If Not IsPostBack Then
    cmvDate.ValueToCompare = DateTime.Now.ToString("d")
End If

不在 Page_Init 事件中.

  1. 将设置 ValueToCompare 的代码移动到 初始化事件,删除对回发的检查.
  2. 检查,在数据检查期间该属性具有什么值(可能是,您应该删除对Not IsPostBack 的检查).
  3. 在检查发生之前设置此属性.
  1. Move the code of setting the ValueToCompare to the Init event, with removing the check for Postback.
  2. Check, what value has this property during data check (may be, you should remove check for Not IsPostBack).
  3. Set this property before check is occured.

这篇关于CompareValidator 的间歇性错误 - ValueToCompare 属性为“"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 21:32