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 4238020
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:51:01+00:00 2026-05-21T02:51:01+00:00

Our project is currently using 2 ways to find a Control inside of pages.

  • 0

Our project is currently using 2 ways to find a Control inside of pages.
The first is to use .FindControl recursively.
The other is to use LINQ like this:

(from n in Page.Controls.Cast<Control>().Descendants(c => c.Controls.Cast<Control>())
 where (n as Label != null && n.ID == "TaskIDLabel")
 select n).First() as Label;

Which uses this Extension:

static public IEnumerable<T> Descendants<T>(this IEnumerable<T> source,
                                            Func<T, IEnumerable<T>> DescendBy)
{
    foreach (T value in source)
    {
        yield return value;

        foreach (T child in DescendBy(value).Descendants<T>(DescendBy))
        {
            yield return child;
        }
    }
}

Which of these 2 methods is better? Which is faster?

  • 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-21T02:51:02+00:00Added an answer on May 21, 2026 at 2:51 am

    Your code and FindControl are functionally different. They do not do the same thing.

    FindControl does not perform a deep search, whereas your code does. From MSDN:

    This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls.

    Which is better? It depends. If you don’t know where on the page a control is, then your recursive methods can find it.

    However, assume that you have two controls in a Panel (ID=”MyPanel”): a custom UserControl (ID=”MyControl”) and a Label (ID=”MyName”). If you call `MyPanel.FindControl(“MyName”), you will get back the expected label in the panel. If you use your function, it will first search within MyControl for a Label with ID=”MyName”. Because of that, if MyControl happens to also contain a label with ID=”MyName”, it will be returned instead. This could be unexpected if a control happens to generate a child control with the same ID as what you are looking for.

    As for performance, your method performs a deeper search so it has the potential to be a much more expensive operation.

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

Sidebar

Related Questions

For our rewrite of the open-source iMedia framework project (in use currently by dozens
Our team is currently working on a large project which makes heavy use of
We are currently using unit tests to test our project. We have the majority
I'm currently using VS2005 Profesional and .NET 2.0, and since our project is rather
Currently, we're using the following version numbering scheme for our C# winforms project: Major
Currently, when building our project we use: javac <various command line options> C:\project\CompilationHelper.java The
I am using MongoDB in our project and I'm currently learning how things work.
We use Subversion as our source control system and store the VisualStudio project files
In our project (which is fully developed using .NET), we use a medium sized
We are currently using a web deployment project to compile the projects in our

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.