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

  • SEARCH
  • Home
  • 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 6541557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:07:09+00:00 2026-05-25T11:07:09+00:00

I have been pondering how I can get all controls on a page and

  • 0

I have been pondering how I can get all controls on a page and then perform a task on them in this related question:

How to Search Through a C# DropDownList Programmatically

I need code that can scan the page, get all DropDownList Controls and return them in a list.

I’m currently having to edit each individual control, I would rather be able to dynamically loop over each control to preform my task.

  • 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-25T11:07:10+00:00Added an answer on May 25, 2026 at 11:07 am

    Check my previous SO answer.

    Basically, the idea is to wrap the recursion of iterating through the controls collection using :

    private void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection)
    where T : Control
    {
        foreach (Control control in controlCollection)
        {
            //if (control.GetType() == typeof(T))
            if (control is T) // This is cleaner
                resultCollection.Add((T)control);
    
            if (control.HasControls())
                GetControlList(control.Controls, resultCollection);
        }
    }
    

    and to use it :

    List<DropDownList> allControls = new List<DropDownList>();
    GetControlList<DropDownList>(Page.Controls, allControls )
    foreach (var childControl in allControls )
    {
    //     call for all controls of the page
    }
    

    [Edited 11/26/2013]: here is a more elegant way to reach this goal. I wrote two extensions methods that can walk the control tree in both directions. The methods are written in a more Linq way as it produces an enumerable:

    /// <summary>
    /// Provide utilities methods related to <see cref="Control"/> objects
    /// </summary>
    public static class ControlUtilities
    {
        /// <summary>
        /// Find the first ancestor of the selected control in the control tree
        /// </summary>
        /// <typeparam name="TControl">Type of the ancestor to look for</typeparam>
        /// <param name="control">The control to look for its ancestors</param>
        /// <returns>The first ancestor of the specified type, or null if no ancestor is found.</returns>
        public static TControl FindAncestor<TControl>(this Control control) where TControl : Control
        {
            if (control == null) throw new ArgumentNullException("control");
    
            Control parent = control;
            do
            {
                parent = parent.Parent;
                var candidate = parent as TControl;
                if (candidate != null)
                {
                    return candidate;
                }
            } while (parent != null);
            return null;
        }
    
        /// <summary>
        /// Finds all descendants of a certain type of the specified control.
        /// </summary>
        /// <typeparam name="TControl">The type of descendant controls to look for.</typeparam>
        /// <param name="parent">The parent control where to look into.</param>
        /// <returns>All corresponding descendants</returns>
        public static IEnumerable<TControl> FindDescendants<TControl>(this Control parent) where TControl : Control
        {
            if (parent == null) throw new ArgumentNullException("control");
    
            if (parent.HasControls())
            {
                foreach (Control childControl in parent.Controls)
                {
                    var candidate = childControl as TControl;
                    if (candidate != null) yield return candidate;
    
                    foreach (var nextLevel in FindDescendants<TControl>(childControl))
                    {
                        yield return nextLevel;
                    }
                }
            }
        }
    }
    

    Thanks to the this keyword, these methods are extensions methods and can simplify the code.

    For example, to find all DropDownList in the page, you can simply call:

    var allDropDowns = this.Page.FindControl<DropDownList>();
    

    Because of the use of the yield keyword, and because Linq is smart enough to defer execution of the enumeration, you can call (for example):

    var allDropDowns = this.Page.FindDescendants<DropDownList>();
    var firstDropDownWithCustomClass = allDropDowns.First(
        ddl=>ddl.CssClass == "customclass"
        );
    

    The enumeration will stop as soon as the predicate in the First method is satisfied. The whole control tree won’t be walked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been pondering on this over the last couple of days. I'm currently
I have been wondering and couldn't find a clear answer on this subject. Imagine
I ran into this RegExp /[[0]]/ in JavaScript, and have been wondering what it
Good morning all. I've been pondering over the best way to use plupload with
I have been coding an application which renders tiles and GUI and all that.
I am new to WP7 programming and I have been following this tutorial http://weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx
I've been programming with detours lately and all that comes with it. I have
I have been developing and tweaking things as much as I can and know
I have been wondering about this, which is why I have put off learning
I have been given the task to evaluate codeFirst and possible to use for

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.