ASP. NET recursive findcontrol

Source: Internet
Author: User
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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.