Alright, I'm going to have a lot of blog posts over the next few days going over my wonderful expereinces learning how to create advanced server controls, and just how hard it is to find good information on the subject. But the problem I ran into today was having Page.FindControl() not work as expected.
Problem
Page.FindControl() can be misleading. A developer without understanding the situation could assume FindControl() will return a control found within that page. This is completely wrong. The reason this does not work is because Page does not have its own FindControl() function. It actually inherits this from Control, because Page inherits control. So FindControl() is really more like a FindChildrenControls(), and it is not recursive.
Example
I have some code that does a Page.FindControl() for a GridView. However, later I move the GridView into an UpdatePanel to add ajax enabled stuff. All of a sudden my Page.FindControl() doesn't work. The reason? Because my GridView is no longer a direct child of the Page and it is now a child of the UpdatePanel. So instead of being Page -> GridView it is Page -> UpdatePanel -> GridView.
Solution
I found a blog post for a recursive post. Here is the code:
-
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;
-
}
Related Posts
- New Dating DNA Front Page Since we're still pending in the Apple iPhone App Store for review, we wanted a new fresh image for the Dating DNA website front page. Kevin, the CEO, threw together a simplified new front page. He is pretty good with Photoshop, and knows how to slice up a layout for...
- ASP .NET 2.0, GridViews, HyperLinkField, and JavaScript I've found some interesting notes on ASP .NET's GridView, it's limitations and work arounds. For work I was looking for a way to execute JavaScript from clicking a link on a GridView Cell. An example would be to having a list of transactions and wanting to click a link to...
- PHP Singletons, Sub-Classing, and HAS-A Relationships I've been very busy these last fews weeks and have neglected making any posts. While there are a variety of subjects I'd love to post about, they'll most likely have to wait until the next year. However, I thought I might be able to throw up a quick example of...
- ASP .NET GridView Access to Data In Code There are situations where you want to gain access to the GridView's DataSet to display extra information outside of the GridView. I found the following code to work the best for me. You can get the information from the DataSource of a GridView by doing the following: DataView dv =...
- OS X and Tabs - Skipping Drop Down Controls Comcast is supposed to come tomorrow to hook up my house, but until then I've been mooching of relatives' WI-FI on my Mac Book Pro. ... Of course the OS X elite call this change simple and easy, but I'd think it'd be easy to just have it work in...

thx for that post, it was quite helpful and made me understand the innner workings of asp.net a little better