简体   繁体   English

隐藏使用Java进行验证的asp.net面板

[英]Hide asp.net panel with validation using Javascript

I need a way to hide/show an asp.net Panel with Validators inside using Javascript. 我需要一种使用Javascript隐藏/显示带有验证器的asp.net面板的方法。

I have successfully hide a panel using this code below: 我已经使用以下代码成功隐藏了面板:

function ShowHidePanel(panelId, isShown) {
    var pnl = document.getElementById(panelId);

    if (pnl != null) {
        if (isShown) {
            pnl.style.display = 'block';
        }
        else {
            pnl.style.display = 'none';
        }
    }
}

I am trying to disable all the validators inside using this code below: 我正在尝试使用以下代码禁用其中的所有验证器:

function ToggleValidator(validatorId, enabled) {
    var validator = document.getElementById(validatorId);
    if (validator != null) {
        ValidatorEnable(validator, enabled);
    }
}

However, even though my panel is hidden, the validation on those validators inside is still firing. 但是,即使我的面板是隐藏的,内部那些验证器上的验证仍在触发。

Any thought will be appreciated. 任何想法将不胜感激。

You'll need jQuery and have to set the ValidationGroup property on your validators, but this should work. 您将需要jQuery,并且必须在验证器上设置ValidationGroup属性,但这应该可以工作。

$("#<%= pnlContainer.ClientID %>")
    .find("span[validationGroup='MyValidationGroup']")
    .each(function () { ValidatorEnable(this, false); });

You are hiding the panel on client side, so it will not have any effect on validators. 您将面板隐藏在客户端,因此不会对验证程序产生任何影响。 They can access the control and hence fire the validations. 他们可以访问控件,从而触发验证。

Disabling validator should work - only issue could be you are passing wrong id. 禁用验证程序应该可以工作-唯一的问题可能是您传递了错误的ID。 You should use ClientID property of server side validator control to refer to validator on client side. 您应该使用服务器端验证程序控件的ClientID属性来引用客户端上的验证程序。

Regardless, another work-around for your issue can be to disable associated controls in the panel when it's hidden. 无论如何,针对您的问题的另一个解决方法是在隐藏面板时禁用面板中的关联控件。 If the control is disabled then validator won't be fired. 如果控件被禁用,则不会触发验证器。

I've run into the same problem and perhaps there's an more elegant fix but I've resorted to using custom validators for 'conditional' validation when needed. 我遇到了同样的问题,也许有一个更优雅的解决方法,但是我在需要时就使用了自定义验证器进行“条件”验证。

For example, I only require SSN if the user selects a SSN radio button. 例如,如果用户选择了SSN单选按钮,则仅要求SSN。

 <asp:CustomValidator ID="cvSsn" runat="server" ControlToValidate="tbSsn"
                                        EnableClientScript="true" Display="Dynamic" ValidateEmptyText="true"
                                        ClientValidationFunction="onValidateSsn" OnServerValidate="OnValidateSSN"
                                        Text="SSN required" ErrorMessage="SSN required" CssClass="validation-error"
                                        ValidationGroup='Company' />

Javascript Client Side Validation: Javascript客户端验证:

function onValidateSsn(sender, args) {
            var rbSsn = $('#<%= rbSsn.ClientID %>');
            args.IsValid = !rbSsn.is(":checked") || isValidSSN(tbSsn.val());

        }

In your case it may look something like the following 您的情况可能类似于以下内容

 function onValidateField(sender, args)
 {
    if (!isShown)
      {
        args.IsValid = true;
        return;
      }

     // preform validation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM