【问题标题】:CustomValidator problem自定义验证器问题
【发布时间】:2026-01-28 00:20:05
【问题描述】:

我的页面上有许多不同类型的 ASP.NET 验证器,其中一些我正在禁用,具体取决于页面上的用户选择,使用 javascript。

$.each(Page_Validators, function(index, validator) 
       {
           if ($(validator).hasClass("pain")) {

               ValidatorEnable(validator, false);

        }
});

这似乎有效,并且验证器在所有情况下都不会触发,除了我在无法使用其他验证器类型时使用的任何 CustomValidators

 <asp:CheckBoxList id="BodyPartsList" runat="server" RepeatColumns = "2" 
                    Width="1023px">
                    <asp:ListItem Text = "0. Head/headaches" Value = "1"></asp:ListItem>
                    <asp:ListItem Text = "1. Leg(s)" Value = "2"></asp:ListItem>
                    <asp:ListItem Text = "2. Arm(s)" Value = "3"></asp:ListItem>
                    <asp:ListItem Text = "3. Neck" Value = "4"></asp:ListItem>
                    <asp:ListItem Text = "4. Shoulder(s)" Value = "5"></asp:ListItem>
                    <asp:ListItem Text = "5. Low Back" Value = "6"></asp:ListItem>
                    <asp:ListItem Text = "6. Upper Back" Value = "7"></asp:ListItem>
                    <asp:ListItem Text = "7. Feet" Value = "8"></asp:ListItem>
                    <asp:ListItem Text = "8. Hand(s)" Value = "9"></asp:ListItem>
                    <asp:ListItem Text = "9. Other(Describe in &quot;Details of Plan&quot;)" Value = "10"></asp:ListItem>
                </asp:CheckBoxList>

                <asp:CustomValidator ID="PainLocationValidator" runat="server" Display="Dynamic"
                ErrorMessage="Location of pain is required." 
                ClientValidationFunction="checkPainListValidate" 
                ValidationGroup = "OnSave" EnableClientScript= "true" SetFocusOnError= "true" Width = "100%" CssClass = "pain" />



 function checkPainListValidate(sender, args) {

    args.IsValid = true;

    if ($('#<%= BodyPartsList.ClientID %> input:checked').length > 0)
        args.IsValid = true;

    else
        args.IsValid = false;

}

为什么会这样?我还能做些什么来禁用它们吗?

谢谢。

【问题讨论】:

    标签: asp.net customvalidator


    【解决方案1】:

    一些 aspx 标记会有所帮助,但无论如何这里有一些想法:

    • 如果您的验证器为空或者您的类型类似于validatorEnable 而不是ValidatorEnable,您是否调试了javascript 以查看发生了什么?您是否收到任何 javascript 错误?
    • CustomValidator 是在服务器端还是在客户端验证,我认为 ValidatorEnable 只会在客户端禁用它
    • EnableClientScript 设置为true
    • 如果您不指定 ControlToValidate,CustomValidator 将在每次回发时进行验证,而不管导致它的事件是什么
    • 您是否提供了在客户端调用的ClientValidationFunction
    • 顺便问一下,你用的是什么浏览器,什么框架,是ASP.Net-Ajax吗?
    • 您是否尝试过通过document.getElementById('&lt;%=MyValidator.ClientID %&gt;').disabled=true; 禁用它?

    我假设您使用错误的 ID 来获取客户端的验证器。

    我已经测试了您的代码,但无法重现此行为:

    这是我禁用所有验证器的 Javascript 函数(已编辑以考虑您的痛苦等级):

     function disablePainValidators() {
         if ((typeof (Page_Validators) != "undefined") && (Page_Validators != null)) {
             var i;
             for (i = 0; i < Page_Validators.length; i++) {
                 if(Page_Validators[i].className=='pain')
                     ValidatorEnable(Page_Validators[i], false);
             }
         }
     }
    

    【讨论】:

    • 感谢您的回复和一些有用的想法 1. 我没有理解第一个想法 2. 我在客户端验证它所以使用 ClientValidationFunction, see the update 3. I do not specify ControlToValidate 因为它是一个复选框列表,如果我得到错误去做。 4. 如果禁用验证器,它是否仍会在每次回发时验证?
    • @Victor:第 1 点是一个错字,我只删除了部分内容(如果您不将 ValidateEmptyText 设置为 true,则永远不会验证空文本,但这对您没有帮助)。我以为你想禁用验证器,那么你为什么要问它是否会在回发时验证?还向我们展示您禁用验证器和 ClientValidationFunction 的 js 代码。调试 js 看看会发生什么。
    • @Tim 是的,我正在尝试禁用验证器,所以只是想知道为什么它仍然处于活动状态并且从未发生回发。这个类有几个验证器,只有 CustomValidator 保持活动状态。
    • @Victor: ControlToValidate 不适用于 CheckBoxList(请参阅我的 #4)。
    • @Tim 我知道这就是我不能使用它的原因