Elegant way to make CustomValidator work with ValidationSummary messagebox

后端 未结 7 2085
耶瑟儿~
耶瑟儿~ 2020-12-14 08:40

I have run into this problem before but never quite solved it. I have a form with several validators and also a CustomValidator.



        
7条回答
  •  有刺的猬
    2020-12-14 09:31

    I've recently had same problem. ValidationSummary was not showing the ErrorMessage from CustomValidator when ServerValidate stated validation failure. Since by default (as my little reverse engineering showed) validation summary is rendered client side on postback I've simply added a script that checks all validators on document load/async postback completion and triggers validation summary creation for failed validation groups:

    $(document).ready(function () {
        var displayAlert = function () {
            if (typeof Page_Validators == 'undefined') return;
    
            var groups = [];
            for (i = 0; i < Page_Validators.length; i++)
                if (!Page_Validators[i].isvalid) {
                    if (!groups[Page_Validators[i].validationGroup]) {
                        ValidationSummaryOnSubmit(Page_Validators[i].validationGroup);
                        groups[Page_Validators[i].validationGroup] = true;
                    }
                }
        };
    
        displayAlert();
    
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
                        function () {
                            displayAlert();
                        });
    }
    );
    

    In my scenario I had nested user controls with validators, update panel and validation summary at the parent page.

    More details here.

提交回复
热议问题