Recursive Page.FindControl

I’m currently writing my first ASP.NET 2.0 website. VS.NET 2005 is worlds better than VS.NET 2003, but I was mildly surprised to find that Microsoft still hasn’t added a recursive overload for Page.FindControl. So, courtesy of Oddur Magnusson, here it is:

private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}

This makes life much easier when you’re trying to get to controls that are themselves contained within other containers, eg, a TextBox inside a DataView or DataList. Would it have killed Microsoft to add an overloaded, recursive version of this?

One interesting semi-undocumented feature of Page.FindControl: you can specify a full “path” to a control using colons as separators, like so:

Page.FindControl(“DataList1:_ctl0:TextBox3”)

That’s assuming all the containers in the hierarchy have explicit names. You can view source to determine what the dynamically generated ‘null’ IDs are.

Jeff Atwood

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Let's be kind to each other. Find me https://infosec.exchange/@codinghorror

⏲️ Busy signing you up.

❗ Something's gone wrong. Please try again.

✅ Success! Check your inbox (and your spam folder, just in case).

Related posts

My Scaling Hero

Inspiration for Stack Overflow occasionally comes from the unlikeliest places. Have you ever heard of the dating website, Plenty of Fish? Markus Frind built the Plenty of Fish Web site in 2003 as nothing more than an exercise to help teach himself a new programming language, ASP.NET. The site

By Jeff Atwood ·
Comments

Wrangling ASP.NET Viewstate

Inspired by Scott Hanselman’s recent post on ASP.NET viewstate wrangling, here’s a roundup of tips for dealing with that ornery viewstate stuff. The first rule of thumb, of course, is to turn it off whenever you can. But sometimes you can’t. I think the DotNetNuke developers

By Jeff Atwood ·
Comments

ASP.NET NTLM Authentication - is it worth it?

At work, we have the luxury of assuming that everyone’s on an intranet. So when it comes to identity management on our ASP.NET websites, NTLM authentication is the go-to solution. Why trouble the user with Yet Another Login Dialog when you can leverage the built in NTLM functionality

By Jeff Atwood ·
Comments
ASP.NET About Box (Page)

ASP.NET About Box (Page)

I had a request for an ASP.NET version of my windows forms About Box. This is a good idea that I’ve considered in the past, so I took the time to convert it today: Clicking details will provide a dump of all loaded assemblies in summary form, with

By Jeff Atwood ·
Comments

Recent Posts

map of the United States via rgmii.org showing all 3,143 counties by rural (gold) / metro (grey) and population

Launching The Rural Guaranteed Minimum Income Initiative

It's been a year since I invited Americans to join us in a pledge to Share the American Dream: 1. Support organizations you feel are effectively helping those most in need across America right now. 2. Within the next five years, also contribute public dedications of time or

By Jeff Atwood ·
Comments
Let's Talk About The American Dream

Let's Talk About The American Dream

A few months ago I wrote about what it means to stay gold — to hold on to the best parts of ourselves, our communities, and the American Dream itself. But staying gold isn’t passive. It takes work. It takes action. It takes hard conversations that ask us to confront

By Jeff Atwood ·
Comments
Stay Gold, America

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood ·
Comments
I’m feeling unlucky... 🎲   See All Posts