必填字段验证器错误地失败了对禁用文本框的验证

发布于 2024-08-07 14:39:49 字数 706 浏览 7 评论 0原文

当关联的文本框被禁用时(无论文本框是否包含文本),必填字段验证器似乎总是会触发。

启用文本框后,验证器将正常运行。

有人能告诉我为什么吗?

我尝试使用 ValidatorEnable 禁用必需的字段验证器,但这似乎没有什么区别。

以下是该页面的相关 HTML(删减):

    <tr id="trBrokerNetID" runat="server">
    <td>
        <cc1:mitextbox id="txtBrokerNetID" runat="server" cssclass="bodytext" width="220px" maxlength="20" onBlur="JavaScript:CheckBrokerBranch(false);"></cc1:mitextbox>
        <asp:requiredfieldvalidator id="rfvBrokerNetID" runat="server" width="1px" errormessage="BrokerNetID - Please supply a value" controltovalidate="txtBrokerNetID">*</asp:requiredfieldvalidator>
    </td>
</tr>

非常感谢收到的任何想法。

A required field validator seems to always to fire when the associated textbox is disabled (whether the textbox contains text or not).

When the textbox is enabled the validator behaves correctly.

Can anybody tell me why?

I've tried disabling the required field validator with ValidatorEnable but that seems to make no difference.

Here's the relevant HTML from the page (cut down):

    <tr id="trBrokerNetID" runat="server">
    <td>
        <cc1:mitextbox id="txtBrokerNetID" runat="server" cssclass="bodytext" width="220px" maxlength="20" onBlur="JavaScript:CheckBrokerBranch(false);"></cc1:mitextbox>
        <asp:requiredfieldvalidator id="rfvBrokerNetID" runat="server" width="1px" errormessage="BrokerNetID - Please supply a value" controltovalidate="txtBrokerNetID">*</asp:requiredfieldvalidator>
    </td>
</tr>

Any ideas gratefully received.

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

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

发布评论

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

评论(3

执手闯天涯 2024-08-14 14:39:49

现在我不知道的是,当在客户端禁用控件时,它不会包含在回发中。

这就是触发服务器端验证的原因。就其而言,控件是空的。

解决方案是使用只读属性而不是禁用属性。

现在要弄清楚如何设置控件的样式,使其具有与禁用时相同的外观

Now what I didn't know was that when a control is disabled on the clientside it doesn't get included in the postback.

Which is why the serverside validation was firing. As far as it was concerned the control was empty.

The soolution is to use the readOnly property rather than the disabled property.

Now to figure out how to style the control to make it have the same look as if it was disabled.

溺深海 2024-08-14 14:39:49

ASP.NET 的验证器以神秘的方式工作:D

首先,使用 ASP.NET 控件的 id 在 jQuery 中访问它是危险的。
如果将控件放置在转发器中或将页面包装在母版页中,则 html 元素的 id 将与您指定的 id 不同。使用类来访问元素。

如果 ASP.NET 验证器希望启用该字段,那么您必须尝试另一种方法。
我的建议是这样的:

1。
将一个类添加到文本框,使其看起来已禁用:

$("#txtBrokerNetID").addClass("thisClassMakesItLookDisabled");

2.
添加一个事件,用于检查文本框的焦点,并在有焦点时将其模糊:

$("#txtBrokerNetID").focus(function() {
  $(this).blur();
});

现在,该字段的行为就像被禁用一样,并且验证器可以工作。

ASP.NET's validators work in mysterious ways :D

First of all it is dangerous to use the id of an ASP.NET control to access it in jQuery.
If you place the control in a repeater or wrap the page in a master page, then the id of the html element will be something different than the id you specified. Use class to access the element instead.

If ASP.NET validators want the field to be enabled then you must try another approach.
My suggestion would be this:

1.
Add a class to the textbox, that makes it looks disabled:

$("#txtBrokerNetID").addClass("thisClassMakesItLookDisabled");

2.
Add an event that checks on focus to the textbox and blurs it if there is focus:

$("#txtBrokerNetID").focus(function() {
  $(this).blur();
});

Now the field behaves as if it is disabled and the validator works.

北方的巷 2024-08-14 14:39:49

您可以选择的一种选项是设置一个 ValidationGroup 而不是表单使用的 ValidationGroup,然后在验证表单时根据需要调用 Page_ClientValidate('text_validation_group')。这样客户端验证就不会妨碍。

One option you can choose, is to set a ValidationGroup than the one the form uses, an then, when validating the form call Page_ClientValidate('text_validation_group') if needed. That way client validation won't get in the way.

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