ASP.net FindControl method Misunderstanding and analysis _ Basic application

Source: Internet
Author: User
1. The scope that the FindControl method is looking for is the descendant control for the given controls.
Copy Code code 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>
</form>

such as the above code, the background with Panel1.findcontrol ("Button1") to find, that such a small range can improve efficiency, in fact, even with Textbox1.findcontrol ("Button1") can also be found. Previously, the FindControl method looks for the corresponding control in the naming container in which it resides based on the ID. When the Textbox1.findcontrol ("Button1") is executed, the ASP. NET to get Textbox1.namingcontainer, the value of the page itself (the last generated Xxxx_aspx class instance), and then recursively look for the corresponding ID of the control, so the same can find Button1.

Similarly, if you use Textbox1.findcontrol ("Label1") you can also find Label1.

2. Don't understand why this. The FindControl method could not find the control in the GridView.

In fact, it is well understood that the FindControl method is searched only under this naming container, and does not go into other naming containers, while the naming container (NamingContainer) is not just the page itself, but also includes GridViewRow, DataListItem, Many controls, such as RepeaterItem, UserControl, masterpage, and so on, inherit the INamingContainer interface, One notable feature of these is that the UniqueID and ClientID of their child controls are generally different from IDs (except for the top-level page objects).

3. It is considered that FindControl operation efficiency is poor.

In fact, asp.net runtime analysis of aspx, ascx, master, and other file tag structure, to produce a tree like the DOM, the general query operation efficiency is relatively high, especially when the problem is not too large. In general, the number of controls in the actual page cannot be thousands, and the search cannot cross this naming container, limiting the size of the problem. So the efficiency of FindControl is not bad.

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.