Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3756904
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:55:26+00:00 2026-05-19T09:55:26+00:00

I am looking for the best way to set the validation group for all

  • 0

I am looking for the best way to set the validation group for all of the controls contained in a user control. Most of the controls in the control are created dynamically when the control is loaded. It has quite a few input fields and validators.

I would like to save time by setting the validation for all the controls and validators with some sort of function that could just loop through everything and set it.

It seems though that there is no consistent interface that includes validation group for all of the different controls that have the property.

Should I use reflection to check for Validation group, I know I can do this, but is there a better way?

We are using C# by the way.

Any help would be appreciated. Thanks!

EDIT: I put the answer down below for anyone who wants the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:55:27+00:00Added an answer on May 19, 2026 at 9:55 am

    Just wanted to mark this answered.

    Here is the code I used to set the validation group. I decided to go with using reflection, since I could check types for those I know have ValidationGroup, and cast and set, but there are a lot of types to check, and it could miss new controls that may be added in the future. Would have been sweet if the ValidationGroup were part of some kind of interface that things had to implement.

    /// <summary>
    /// this is an extension method to iterate though all controls in a control collection
    /// put this in some static library somewhere
    /// </summary>
    /// <param name="controls"></param>
    /// <returns></returns>
    public static IEnumerable<Control> All(this ControlCollection controls)
    {
        foreach (Control control in controls)
        {
            foreach (Control grandChild in control.Controls.All())
                yield return grandChild;
    
            yield return control;
        }
    }
    
    
    /// <summary>
    /// this function uses reflection to check if the validation group exists, and then set it to the 
    /// specified string
    /// </summary>
    /// <param name="ValidationGroup"></param>
    private void SetValidationGroup(string ValidationGroup)
    {
        //set the validation group for all controls
        if (ValidationGroup.IsNotNullOrEmpty())
        {
            foreach (Control c in Controls.All())
            {
                var Properties = c.GetType().GetProperties();
                var vg = Properties.Where(p => p.Name == "ValidationGroup").SingleOrDefault();
                if (vg != null)
                {
                    vg.SetValue(c, ValidationGroup, null);
                }
    
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for the best way to set-up an iPhone project in XCode ... namely:
I am looking for the best or any way to set the Album Art
I am looking for the best way to dynamically arrange the location of labels
Looking for the best way to do ownership validation in a Rails model without
I'm looking for a best way to represent a set of structural data. I'm
I am looking for the best way to set a list item's currentmenu class,
I'm looking for best way of using session within zf application. At first I
I'm looking for the best way to log errors in an ASP.NET application. I
I'm looking for the best way to take a simple input: echo -n Enter
I am looking for the best way to test if a website is alive

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.