Triggering multiple validation groups with a single button?

后端 未结 3 1992
闹比i
闹比i 2020-12-16 16:54

Let\'s say the page TestPage.aspx has two controls. The first control is an address control that has a validation group called \"AddressGroup\". This group contains severa

相关标签:
3条回答
  • 2020-12-16 17:30

    Are you talking client side or server side validation? Jamie's answer is spot on for server side, but for client side validation you will probably need to write your own JS function that will trigger validation on all three groups in concert.

    0 讨论(0)
  • 2020-12-16 17:31

    Call Page.Validate() on server side it will validate all the validators..

    0 讨论(0)
  • 2020-12-16 17:40

    Call the Validate method for each validation group individually inside the button's click handler:

    bool isValidTest = false;
    Validate("AddressGroup");
    isValidTest = IsValid;
    Validate("CreditCardGroup");
    isValidTest &= IsValid;
    // etc.
    if (!isValidTest) return;
    

    The next problem you may encounter is that the ValidationSummary control is linked to a single validation group. The only way that I've found to display all the error messages for multiple groups (without walking the control tree) is use multiple ValidationSummary controls.

    With user controls, you may want to have its Validate method perform validation for all the controls it contains and display its own summary.

    Edited to add: The isValidTest variable is not needed. According to the docs:

    Note that when you call the Validate method, the IsValid property reflects the validity of all groups validated so far.

    0 讨论(0)
提交回复
热议问题