You can walk up the control tree on a page by means of the NamingContainer property. In contrast to the Container keyword, which is available only in inline code (that is, in a <%# and %> tag), the NamingContainer property is available in code to any instance of that class or of a derived class.
The following code example illustrates how to walk the control tree of an ASP.NET Web page. The button's
ChangeBtn_Click method handler searches for a control using the FindControl method and then determines the NamingContainer object for that control. It then determines the naming container for the control returned by the first call to the NamingContainer property, and so on up the control tree until it finds a control that has no naming container. (Note that the
WalkContainers method will also add the type of the control at the lowest level, which is not itself a naming container.)