FindControl method misunderstanding and Analysis

Source: Internet
Author: User

1. The FindControl method is used to find the range of the Child Control of the given Control.
Copy codeThe Code is as follows: <form id = "form1" runat = "server">
<Asp: Label ID = "Label1" runat = "server" Text = "Label"> </asp: Label>
<Asp: Panel ID = "Panel1" runat = "server">
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: Button ID = "Button1" runat = "server" Text = "Button"/>
</Asp: Panel>

As shown in the above Code, Panel1.FindControl ("Button1") is used in the background to search for it. The smaller range can improve efficiency. In fact, TextBox1.FindControl ("Button1") can also be found. As mentioned earlier, the FindControl method is to find the corresponding Control in the name container where the Control is located based on the ID. When TextBox1.FindControl ("Button1") is executed, ASP. NET first obtains TextBox1.NamingContainer, whose value is the page itself (the last generated xxxx_aspx class instance), and then recursively searches for the control of the corresponding ID, so Button1 can be found in the same way.

Similarly, if TextBox1.FindControl ("Label1") is used, Label1 can also be found.

2. I don't understand why the controls in the GridView cannot be found in this. FindControl method.

In fact, it is easy to understand that the FindControl method is only searched under the naming container, and will not be searched in other naming containers, while the naming container (NamingContainer) is not just the page itself, it also includes GridViewRow, DataListItem, RepeaterItem, UserControl, MasterPage, and many other controls that inherit the INamingContainer interface, one of their notable features is that the UniqueID and ClientID of its child controls are generally different from the ID (except the top-level page object ).

3. The FindControl operation efficiency is poor.

ASP. NET runtime analysis of aspx, ascx, master and other file label structure, generate a Dom-like control tree, the query efficiency of the tree is generally relatively high, especially when the problem scale is not large. In general, the number of controls on the actual page cannot be tens of thousands, and the search cannot go beyond the named container. These factors limit the size of the problem. Therefore, FindControl is not efficient.

Related Article

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: 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.