当 ASP.NET 忽略 CustomValidator 时您会怀疑什么?

发布于 2024-07-09 05:45:54 字数 1325 浏览 6 评论 0原文

这既是代码维护问题,也是代码问题,但我有一个 WebForm,它不再检查 CustomValidator。 当我一年多前最后一次接触代码时,它起作用了,但现在用户请求了一些更改,它不再起作用了……

WebForm 包含一个数据绑定下拉列表,其中包含一个带有字符串的默认“-All-”项目。空如其值。 当用户单击提交按钮时,验证器应检查下拉列表的值是否不是 String.Empty。 我在客户端验证代码和服务器验证代码中设置了断点,但都没有触发。

你会从哪里开始寻找? 通常的嫌疑人有哪些? 当然,我已将我的工作副本与源代码管理中的内容进行了比较,但没有发现任何可疑之处。

以防万一,这是我的代码:

<asp:DropDownList ID="_AssessmentDropDown" runat="server" DataSourceID="_AssessmentsData" CausesValidation="true" AutoPostBack="false"
    DataTextField="AssessmentName" DataValueField="AssessmentName" OnDataBound="_HandleAssessmentsBound">
</asp:DropDownList>
<asp:CustomValidator ID="_AssessmentValidator" runat="server" ClientValidationFunction="_HandleValidateAssessment_Client"
    ControlToValidate="_AssessmentDropDown" ErrorMessage="* You must select an Assessment."
    OnServerValidate="_HandleValidateAssessment" />
<asp:ObjectDataSource ID="_AssessmentsData" runat="server"
    OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
    TypeName="DataTableAdapters.GET_GRADE_ASSESSMENTSTableAdapter">
    <SelectParameters>
      <asp:ControlParameter Name="GRADECODE" ControlID="_GradeCodeDropDown" PropertyName="SelectedValue" />
    </SelectParameters>
</asp:ObjectDataSource>

This is as much a code maintenance issue as a code issue, but I have a WebForm that no longer checks it CustomValidator. It worked when I last touched the code over a year ago, but it no longer works now that the user has requested some changes ...

The WebForm contains a data-bound drop down with a default " - All -" item with String.Empty as its value. When the user clicks the submit button, the validator should check that the drop down's value is not String.Empty. I've set break points in the client validation code and the server validation code, but neither fire.

Where would you start looking? What are the usual suspects? I have, of course, compared my working copy to what is in source control, but nothing jumps out as being suspicious.

Just in case it matters, here is my code:

<asp:DropDownList ID="_AssessmentDropDown" runat="server" DataSourceID="_AssessmentsData" CausesValidation="true" AutoPostBack="false"
    DataTextField="AssessmentName" DataValueField="AssessmentName" OnDataBound="_HandleAssessmentsBound">
</asp:DropDownList>
<asp:CustomValidator ID="_AssessmentValidator" runat="server" ClientValidationFunction="_HandleValidateAssessment_Client"
    ControlToValidate="_AssessmentDropDown" ErrorMessage="* You must select an Assessment."
    OnServerValidate="_HandleValidateAssessment" />
<asp:ObjectDataSource ID="_AssessmentsData" runat="server"
    OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
    TypeName="DataTableAdapters.GET_GRADE_ASSESSMENTSTableAdapter">
    <SelectParameters>
      <asp:ControlParameter Name="GRADECODE" ControlID="_GradeCodeDropDown" PropertyName="SelectedValue" />
    </SelectParameters>
</asp:ObjectDataSource>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

为人所爱 2024-07-16 05:45:54

我注意到几个问题,

  • 如果 AutoPostBack 设置为 false,我认为您不需要 CausesValidation=true
  • 您不使用验证组,因此这不可能是原因
  • 为什么不使用RequiredFieldValidator?
  • 如果要在空字段上触发验证,请将 ValidateEmptyText 属性设置为 true

I notice a couple of issues

  • I don't think you need a CausesValidation=true if AutoPostBack is set to false
  • You do not use validation groups, so that cannot be the cause
  • Why not use a RequiredFieldValidator?
  • If you want to fire validation on empty fields, set the ValidateEmptyText property to true
绝不服输 2024-07-16 05:45:54

如果要验证的控件具有空值,则 CustomValidator 不会触发,因此 CustomValidator 应始终与RequiredFieldValidator 一起使用

A CustomValidator doesn't fire if the control it is validating has an empty value, so a CustomValidator should always be accompanied by RequiredFieldValidator

梦旅人picnic 2024-07-16 05:45:54

一些故障排除步骤:

  • 这是表单上唯一的验证器吗?
  • 页面上是否启用了验证?
  • 是否为目标对照启用了验证?
  • 验证器本身是否已启用?

Some troubleshooting steps:

  • Is this the only validator on the form?
  • Is validation enabled on the page?
  • Is validation enabled for the targeted control?
  • Is the validator itself enabled?
能怎样 2024-07-16 05:45:54

我会认真研究 ValidationGroup。

如果某些东西被排除在组之外,它就不再有效。 否则,请确保没有任何 JavaScript 错误(对于客户端)并且“OnServerValidate”方法内部有断点。

I would take a serious look at the ValidationGroup.

If something has been left out of the group, it wouldn't validate anymore. Otherwise, make sure that you don't have any javascript error (for the client side) and that the method that is "OnServerValidate" has a break point inside.

逆光下的微笑 2024-07-16 05:45:54

验证器是否与提交按钮位于同一验证器组中?

Is the validator in the same validator group as the submit button?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文