In general, the findcontrol function can only find the first layer of child controls, such as the gridview, formview and other compound controls. It is very troublesome to find the child controls that contain them. To find the parent control (container ), you can use findcontrol to find the child control. In addition, once all parent controls are changed at any layer, they need to be modified again.
Program Code This is very troublesome.
To solve this problem, you can use recursive methods to perform findcontrol operations. See the findcontrolex method below.
Code
/// <Summary>
/// Recursively searches for controls with a specified ID
/// </Summary>
/// <Param name = "parent"> Parent control (control container) </Param>
/// <Param name = "ID"> Control ID to be searched </Param>
/// <Returns> Return the ID-compliant control. If not found, return null. </Returns>
Public Static System. Web. UI. Control findcontrolex (system. Web. UI. Control parentcontrol, String ID)
{
System. Web. UI. Control = Null ;
// First use findcontrol to find the specified child Control
Control = Parentcontrol. findcontrol (ID );
// If not found, go to the layer-down recursive mode to find
If (Control = Null )
{
Foreach (System. Web. UI. Control ochildctrl In Parentcontrol. Controls)
{
// Call back the original function recursively
Control = Findcontrolex (ochildctrl, ID );
// Exit the loop if the specified control is found.
If (Control ! = Null ) Break ;
}
}
Return Control;
}